× Home
Next Lec → ← Previous Lec

Call by value & Call by reference

Actual and Formal parameters

Let's quickly discuss pointers:
A pointer in C is a variable that allocates memory dynamically. It holds the value that is the address of another variable, i.e., direct address of the memory location. The syntax of a pointer variable declaration is type *variable_name;.

Here, * is used to denote the pointer variable, and to return the address of the variable, we use operator '&'.

Types of Function Calls in C programming

Call by value

In this method, the 'call by value' of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In this function call, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. Actual and formal arguments will be created in a different memory location. The following program is the example of 'Call by Value'.

                        

                            void swap(int x, int y)
                            {
                                int temp;
                                temp = x;
                                x = y;
                                y = temp;
                            }
                            void main()
                            {
                                int r = 10, v = 20;
                                swap(r, v); // passing value to function
                                printf("\nValue of r: %d", r);
                                printf("\nValue of v: %d", v);
                            }
                        
                    

Call by reference

In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses we could access the actual arguments and hence we would be able to manipulate them. The changes that are made to the parameter affect the argument. This is because the address is used to access the actual argument. Formal and actual arguments will be created in the same memory location. The following program is the example of 'Call by Reference'.

                        

                            void swap(int *x, int *y)
                            {
                                int temp;
                                temp = *x;
                                *x = *y;
                                *y = temp;
                            }
                            void main()
                            {
                                int r = 10, v = 20;
                                swap(&r, &v); // passing value to function
                                printf("\nValue of r: %d", r);
                                printf("\nValue of v: %d", v);
                            }
                        
                    
                            

                                void changeValue(int* address)
                                {
                                *address = 37565;
                                }
                                
                                int main()
                                {
                                int a = 34, b =56;
                                printf("The value of a now is %d\n", a);
                                changeValue(&a);
                                printf("The value of a now is %d\n", a);
                                return 0;
                                } 
                            
                        

//output
The value of a now is 34
The value of a now is 37565

Ecercise

Given tow numbers a and b, add them then subtract them and assign them to a and b using call by reference.
a = 4
b = 3
after running the function:
a = 7
b = 1

                        

                                #include <stdio.h>
                                void ads(int *vala, int *valb)
                                {
                                    int temp;
                                    temp = *vala + *valb;
                                    *vala = temp;
                                    temp = *vala - *valb - *valb;
                                    *valb = temp;
                                }
                                int main()
                                {
                                    int a = 4, b = 3;
                                    ads(&a, &b);
                                    printf("The value of a is : %d and the value of b is : %d", a, b);
                                    return 0;
                                }