-->

Header Ads

Different Parameter Passing Technique.

Parameter passing in program is most important thing. it gives flexibility to programmer for transferring variable from one function to another function. specially when our program size increase then the no of variables also increased so that our aim to reduce no of variable (because generally variable decide the size of program in context of space ) and as possible as use variable in efficient manner. here we will see different parameter passing technique which used in programming.

  1. Call By Value
  2. Call By Reference
  3. Call By Need
  4. Call By Name
  5. Call By Value Result 
Most of programming language support first two method i.e call by value and call by reference. C,C++,Java they support first two method. call by name method used in ALGOL, LISP, PROLOG.
Before discussing all these method i want to give you some idea about Parameter. there are two type of parameter:
1. Actual parameter: the value passed to any function within main() function block is called that is called actual parameter.
2.Formal Parameter:  the value passed in function which is defined outside of main block is called formal parameter.
Example:

              
               
1.Call By Value :  In this technique Actual parameter and Formal parameter have different memory location .e.g: below given example i and j variable define in main function whereas a,b,and c define in swap function.we call swap function to swap value.
                                                                                       now come to call by reference ,we know actual and formal parameter have different memory location that means if we change value of i and j then a and b directly not affected.Here initially i = 10 and j = 20 two memory location created. when we declare formal parameter then a=20 and b=10 two different memory location created. after calling swap function the value of i and j remains unchanged.because their memory address is different.
so when we print(i,j)=> 10,20 gives.
                    print(a,b)=>20,10 gives




2.Call By Reference : In this technique actual parameter and formal parameter have same memory location .
 below given example i and j initially have assign some memory location. but when we call a swap function than we pass address of i and j as argument  which referenced by a and b variable so that any changes made in i and j or a and b is affected both argument(actual&formal).
so the output will be as print(i,j)=> 20,10 gives.
           print(a,b)=>20,10 gives 


3.Call By Need :it is same as call by value that means actual parameter and formal parameter have different memory locations.but one thing is different that value of actual parameter get copied into formal parameter when needed. In shown example 
                                                                      you can see the variables have different memory location initially a have value 10 after that sample function called, in sample function a is incremented by one so  a = 11 and print this value. now x is called then new value of a is pass to x i.e x=11 and after this x is incremented so x=12 and print the value of x. now return to main function and print the value of a which is a=11.


4.Call By Name : Its like macro substitution. during substitution formal parameter get replaced by actual parameter. 
below you can see that whole bunch of called function sample() is shifted in to main and formal parameter act as actual parameter. program execute in this manner. initially a=10 after increment a=11 and then print(a)=>11. now x comes in role but x is also referencing a so xis incremented that means a is also increment.so print(x)=>12. at the end print(a)=> 12. 
2.Call By Value Result: its also like call by value i.e. there will be different memory location for actual and formal parameter. actual parameter get copied in to formal parameter when call function.at the end of function call ,value of formal copied in to actual parameter. 
in given example : initially a=10 ,incremented by 10 so a=20 and print(a)=>20. next line x is incremented by 10 so x=20 and print(x)=>20.again a is incremented by 10 so a=30but at the end of function call the value of x is copied in to a so a will become a=20.



No comments

Powered by Blogger.