Parameters in function call in c msbte | Call by value in c msbte | Call by reference in c msbte
Parameters in function call in c msbte | Call by value in c msbte | Call by reference in c msbte |
Parameters in Function Call
- When a function gets called there are two possibilities of order in which the actual parameters are passed to the function definition and maps with formal parameters. The possibilities are:
- Parameters are passed from left to right.
- Parameters are passed from right to left.
- In C. parameters are passed from right to left. The order of parameter passing doesn't much essential for certain examples like following function call:
- cwpedia_Sum(m,n,o.p):
- It doesn't matter in which order the m, n, o and p are passed after encountering this function call, but order of passing parameters matters a lot for certain examples like following:
- cwpedia_Power(x,y);
- Parameters in a function call can be passed in two ways.
- The method of passing data between functions is called as parameter passing. Parameter passing can be achieved in two ways.
- Methods of parameter passing
- Call by value
- Call by reference
Call by value
- The method of calling function by passing values to it,is referred as call by value.
- In this method only values of the actual parameters are copied into formal parameters. So if any changes are done with formal parameters it can't affect the actual parameters.
- Function call made by using call by value method can't able to reflect the changes done inside the functions on actual parameters.
- Lets See The Following Example:-
- #include<stdio.h>
- #include<conio.h> //Contain getch() function
- void cwpedia_Fun(int);
- int main()
- {
- int num=10; //num of main()= 10
- cwpedia_Fun(num); //10 passes to cwpedia_Fun
- printf("\nValue after function call: %d", num); //print value of num of main()
- getch(); //Holds The Output screen
- }
- void cwpedia_Fun(int num) //10 copied into parameter num
- {
- num=num + 10;
- printf("Value inside function: %d", num); //prints updated value of num of cwpedia_Fun
- }
- Output:-
- Value inside function: 20
- Value after function call: 10
- Explanation
- Here compiler copies value of actual parameter num i.e.10 into parameter num.
- Even though actual and formal parameter has same names, compiler treats both as different variables.
- Different spaces are allocated to both parameters. It leads wastage of memory.
- The scope of parameter num is limited with the cwpedia_Fun () function only.
- So the parameter num is incremented by 10 and prints modified value ie. 20. The } brace of Function cwpedia_Fun () leads the control back to the called function ie. main() and continue the execution from net statement after function call.
- After function call, the printf() function in main will print value of actual parameter num i.e.10 becaust modifications can't get replicated on actual parameter.
Call by Reference
- The method of calling function by passing reference of variable to it is referred as call by reference.
- Here reference indicates the memory address of Variable where it is stored.
- In this method instead of values, location number of actual parameter in the memory is passed to formal parameters. So all the changes done within the function are reflected in the actual parameters also.
- Function call made by using call by reference method able to reflect the changes done inside the functions on actual parameters.
- Syntax of call by reference:
- To call a function using call by reference we us following syntax:
- Function_Name (&argument_namel, &argument_name2,....)
- Here each parameter/argument name prefixed by the address of (&) sign. Rest all is same as the function call using call by value method. Also the function definition has some little changes as follows:
- return_type function_name (type1 *argument_name1,type2 *argument_name2,...)
- The * sign is used to indicate it holds only memory location. In this type of variables are called as pointers.
- Lets See The Following Example:-
- #include<stdio.h>
- #include<conio.h> //Contain getch() function
- void cwpedia_Fun(int *);
- int main()
- {
- int num=10; //num of main()= 10
- cwpedia_Fun(&num); //Address of num passes to cwpedia_Fun()
- printf("\nValue after function call: %d", num); //print value of num modified by cwpedia_Fun()
- getch(); //Holds The Output screen
- }
- void cwpedia_Fun(int *p) //Address of num copied into parameter num
- {
- *p=*p + 10; //Addtion is done at memory location of num variable Address
- printf("Value inside function: %d", *p);
- }
- Output:-
- Value inside function: 20
- Value after function call: 20
- Explanation
- Here compiler passes address of actual parameter num (let's say 1002) to parameter *p.
- The parameter p stores address of num. *p denotes the value stored at location given in p i.e. value stored at location 1002 which is 10.
- So the statement *p = *p + 10 will add 10 to the value 10 of variable num. So the value at location 1002 becomes 20.
- So printf() statement in the function fun prints modified value i.e. 20. The } brace of function cwpedia_Fun() leads the control back to the called function i.e. main () and continue the execution from next statement after function call.
- As the modifications are made directly to the location where num is stored, the printf() statement after function call will print modified value i.e. 20.
Post a Comment