The document explains two methods of passing data to functions in C: call by value and call by reference. In call by value, the actual parameter's value is copied, and changes made in the function do not affect the original variable, while in call by reference, the address of the variable is passed, allowing modifications to the original variable. Additionally, it briefly introduces recursion with an example of calculating factorial using a recursive function.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
34 views13 pages
Call by Value and Call by Reference in
The document explains two methods of passing data to functions in C: call by value and call by reference. In call by value, the actual parameter's value is copied, and changes made in the function do not affect the original variable, while in call by reference, the address of the variable is passed, allowing modifications to the original variable. Additionally, it briefly introduces recursion with an example of calculating factorial using a recursive function.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
Call by value and Call by reference in C
• There are two methods to pass the data into
the function in C language, i.e., call by value and call by reference. Call by value in C
• In call by value method, the value of the actual
parameters is copied into the formal parameters. In other words, we can say that the value of the variable is used in the function call in the call by value method. • In call by value method, we can not modify the value of the actual parameter by the formal parameter. • In call by value, different memory is allocated for actual and formal parameters since the value of the actual parameter is copied into the formal parameter. • So any changes made inside called function is not reflected in actual parameters of the calling function. #include<stdio.h> void change(int num) { printf("Before adding value inside function num=%d \n",num); num=num+100; printf("After adding value inside function num=%d \n", num); } int main() { int x=100; printf("Before function call x=%d \n", x); 100 change(x); //passing value in function printf("After function call x=%d \n", x); 100 return 0; } Output Before function call x=100 Before adding value inside function num=100 After adding value inside function num=200 After function call x=100 #include<stdio.h> void swapx(int x, int y); int main() { int a = 10, b = 20; // Pass by Values swapx(a, b); printf("a=%d b=%d\n", a, b); return 0; } // Swap functions that swaps // two values void swapx(int x, int y) { int t; t = x; x = y; y = t; printf("x=%d y=%d\n", x, y); Output x=20 y=10 a=10 b=20 Call by reference While calling a function, instead of passing the values of variables, we pass address of variables (location of variables) to the function known as “Call By References”. #include <stdio.h> void main () { int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); /* calling a function to swap the values */ swap(&a, &b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); } void swap(int *x, int *y) { int temp; temp = *x; /* save the value of x */ *x = *y; /* put y into x */ *y = temp; /* put temp into y */ Output:
Before swap, value of a : 100
Before swap, value of b : 200 After swap, value of a : 200 After swap, value of b : 100 Recursion function • A recursive function is a function that calls itself during its execution. The process may repeat several times, outputting the result and the end of each iteration. #include <stdio.h> int factorial(int n) { if ( n == 1) return 1; else return(n * factorial(n - 1)); } int main() { int n, result; printf("Enter a number to find it's Factorial: "); scanf("%d", &n); result = factorial(n); printf("The Factorial of %d is %d.\n", n, result); return 0; Write a program to c language to print Fibonacci series up to 100 by using recursion function.