Call by value and call by reference in C
Hello coders! Welcome back!
Today will look into what is a function call by value and call by reference. Let's get started!
Before starting there is a little prerequisite that you should know that is what is the formal parameter and actual parameter. If you are unaware about this then click here to know in detail about the formal parameter and actual parameter.
Today will look into what is a function call by value and call by reference. Let's get started!
Before starting there is a little prerequisite that you should know that is what is the formal parameter and actual parameter. If you are unaware about this then click here to know in detail about the formal parameter and actual parameter.
Call by value-
Here values of the actual parameter will be copied to formal parameters and these two parameters store values in a different location.
Changes made in the formal parameters won't affect the actual parameter
Let's understand this with an example-
Here, you can see that both variables are stored at a different location. And by changing the values it won't affect the actual parameter values.
In call by value method, we just pass the values to the function called.
When we execute-
Line no. 12 → we initialise x with value 40. Assume that it is stored in 1000 memory location.
Line no. 13 → we initialise y with value 70. Assume that it is stored in 2000 memory location.
Line no. 14 → It is important to note that we have used the '&' symbol in the actual parameter list. It means that we are passing the addresses of those variable to the 'test' function definition.
Line no. 3 → Now the control passes to the function definition. It's important to note that we used the '*' symbol in the formal parameter list. It is known as pointers. Will deal with pointers in later blogs. For time being pointers are those variables which are capable of holding the addresses.
Line no. 5 → if we mention
Call by reference-
Here both actual and formal parameters refer to the same memory location. Therefore, any changes made in formal parameters will get reflected to actual parameters.
Here instead of passing values, we pass the addresses of the variable.
Let's understand this with an example-
Let's look at the explanation line by line.
When we execute-
Line no. 12 → we initialise x with value 40. Assume that it is stored in 1000 memory location.
Line no. 13 → we initialise y with value 70. Assume that it is stored in 2000 memory location.
Line no. 14 → It is important to note that we have used the '&' symbol in the actual parameter list. It means that we are passing the addresses of those variable to the 'test' function definition.
Line no. 3 → Now the control passes to the function definition. It's important to note that we used the '*' symbol in the formal parameter list. It is known as pointers. Will deal with pointers in later blogs. For time being pointers are those variables which are capable of holding the addresses.
Line no. 5 → if we mention
x=70 it means that we are accessing the value
here we are mentioning
*x=70 which means that we access the value of that particular address. And changing the value to 70
Line no. 6 → same operation happens as mentioned for '*y'
line no. 15 → Now the control passes to the main function and we print the changed value.
Hope so you got a clear idea about function call by value and call by reference.
If any doubts drop a comment always ready to clear them.
Happy learning!
here we are mentioning
*x=70 which means that we access the value of that particular address. And changing the value to 70
Line no. 6 → same operation happens as mentioned for '*y'
line no. 15 → Now the control passes to the main function and we print the changed value.
Hope so you got a clear idea about function call by value and call by reference.
If any doubts drop a comment always ready to clear them.
Happy learning!
Comments
Post a Comment