0% found this document useful (0 votes)
24 views

Function Call by Value in C Programming

Function call by value is the default way to call functions in C where the values of actual parameters are copied to formal parameters. Any changes to formal parameters do not affect actual parameters. Function call by reference passes the addresses of actual parameters, allowing changes to formal parameters to affect actual parameters. An example demonstrates calling a function by reference to swap the values of two variables by dereferencing address pointers with *var within the function.

Uploaded by

Vasuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Function Call by Value in C Programming

Function call by value is the default way to call functions in C where the values of actual parameters are copied to formal parameters. Any changes to formal parameters do not affect actual parameters. Function call by reference passes the addresses of actual parameters, allowing changes to formal parameters to affect actual parameters. An example demonstrates calling a function by reference to swap the values of two variables by dereferencing address pointers with *var within the function.

Uploaded by

Vasuki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Function call by value in C programming

Function call by value is the default way of calling a function in C programming.


Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function declarations.
Example:
#include <stdio.h>
int sum(int a, int b)
{
int c=a+b;
return c;
}
int main()
{
int var1 =10;
int var2 = 20;
int var3 = sum(var1, var2);
printf("%d", var3);
return 0;
}
26-11-2019 SNS College of Technology Ms.J.Vasuki,AP/IT 1
What is Function Call By value?
When we pass the actual parameters while calling a function then this is known as
function call by value.
In this case the values of actual parameters are copied to the formal parameters. Thus
operations performed on the formal parameters don’t reflect in the actual parameters.

26-11-2019 SNS College of Technology Ms.J.Vasuki,AP/IT 2


Function call by reference in C programming

When we call a function by passing the addresses of actual parameters then this way of calling the function is
known as call by reference. In call by reference, the operation performed on formal parameters, affects the
value of actual parameters because all the operations performed on the value stored in the address of actual
parameters. It may sound confusing first but the following example would clear your doubts.
Example:
#include <stdio.h>
void increment(int *var)
{
*var = *var+1; Output:
} Value of num is: 21
int main()
{
int num=20;
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
26-11-2019 SNS College of Technology Ms.J.Vasuki,AP/IT 3
Swapping numbers using Function Call by Value
#include <stdio.h>
void swapnum( int var1, int var2 )
{
int tempnum ;
tempnum = var1 ; Output:
var1 = var2 ;
var2 = tempnum ; Before swapping: 35, 45
} After swapping: 35, 45
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2);
swapnum(num1, num2);
printf("\nAfter swapping: %d, %d", num1, num2);
}

26-11-2019 SNS College of Technology Ms.J.Vasuki,AP/IT 4


Function Call by Reference – Swapping numbers

#include
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ; Output:
*var2 = tempnum ;
} Before swapping:
int main( ) num1 value is 35
{ num2 value is 45
int num1 = 35, num2 = 45 ; After swapping:
printf("Before swapping:"); num1 value is 45
printf("\nnum1 value is %d", num1); num2 value is 35
printf("\nnum2 value is %d", num2);
swapnum( &num1, &num2 );
printf("\nAfter swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
return 0;
}

26-11-2019 SNS College of Technology Ms.J.Vasuki,AP/IT 5

You might also like