Passing parameter / Argument: – We have three passing parameter technique. By this technique calling function always passes the argument to the called function, we can also say that the passing parameter technique are used for massage passing between two function. There are three techniques in which we can pass the argument –
- call by address
- call by value
- call by reference
Call by value: – By this technique be pass or call the called function from calling function by value (Either in direct value or expression).
The called function always works on a separate copy of actual argument so any changes inside called function will not affect the argument of calling function.
- Calling function has function call statement. Function call has actual argument.
- Called function always has formal argument.
Call by address: – By this technique we pass address of the actual argument to the formal argument. Formal argument works on address of actual argument works on address of actual argument rather than the value of actual argument. Show any changes made with formal argument will permanently change the value of actual argument.
At the place of actual argument the address will be passed and the formal argument that will receive address, the formal argument must be a type of pointer.
int a=6,*ptr; prt=&a; OR int a=6,*ptr=&a;
Reference operator (&): – This operator is use by reference variable. Reference variable in an alternative or alias of previously created variable. The address of reference variable and the previously declared variable will be exactly same so any changed made with any of to the value will permanently change.
int a=6; int &b=a; OR int a=6; int &b; b=a;
Note: – Reference variable must only be at L.H.S. of assignment operation.
Call by reference: – By this technique, we pass actual value as actual argument from calling function to the reference variable as formal argument of called function.
Any changes made with formal argument will permanently change the actual argument because formal argument works on address of actual argument.
Note: – Call by reference technique is combination of call by value and call by address.