YouTip LogoYouTip

C Function Call By Pointer

# C Call by Reference [![Image 3: C Functions](#) C Functions](#) In call by reference, the formal parameter is a pointer to the address of the actual parameter. When operating on the pointed-to value of the formal parameter, it is equivalent to operating on the actual parameter itself. Passing pointers allows multiple functions to access the object referenced by the pointer without declaring the object as globally accessible. /* Function definition */void swap(int *x, int *y){ int temp; temp = *x; /* Save the value at address x */ *x = *y; /* Assign y to x */ *y = temp; /* Assign temp to y */ return;} For more details on pointers in C, please visit the (#) chapter. Now, let's call the function **swap()** by reference: ## Example #include /* Function declaration */ void swap(int*x, int*y); int main () { /* Local variable definition */ int a =100; int b =200; printf("Before swap, value of a: %dn", a ); printf("Before swap, value of b: %dn", b ); /* Call function to swap values * &a indicates pointer to a i.e. address of variable a * &b indicates pointer to b i.e. address of variable b */ swap(&a, &b); printf("After swap, value of a: %dn", a ); printf("After swap, value of b: %dn", b ); return 0; } When the above code is compiled and executed, it produces the following result: Before swap, value of a: 100Before swap, value of b: 200After swap, value of a: 200After swap, value of b: 100 The example above shows that, unlike call by value, call by reference changes the values of a and b within the function, which actually also changes the values of a and b outside the function. [![Image 4: C Functions](#) C Functions](#)
← Python3 Func Number LogPython3 Func Number Floor β†’