This document discusses function call by value and reference in C programming. It provides examples of swapping integer values using each method. Call by value passes the actual value of an argument, so changes made to parameters inside the function do not affect the argument. Call by reference passes the address of an argument, allowing the function to access and modify the original argument value. The key differences are that call by value uses general variables as arguments while call by reference uses pointer variables, and only call by reference reflects changes to formal parameters in the actual parameters.
3. User Defined Function
A user-defined function is a function provided by the
user of a program or environment, in a context where
the usual assumption is that functions are built into
the program or environment.
Syntax:
return_type function_name (parameter list)
{
Function Body
return statement;
}
3
4. Common Method of Swapping Two Integers
4
#include<stdio.h>
main()
{
int x, y, z;
printf (“n Enter the value of x: ");
scanf ("%d", &x);
printf (“n Enter the value of y: ");
scanf ("%d", &y);
z = x;
x = y;
y = z;
printf ( "n After swap : x = %d, y = %d n", x, y);
return 0;
}
Using Third Variable
Source Code:
Storing Value
Storing Value
Swap using 3rd
Variable
Declaring Variable
6. 6
Call by Value
The call by value method of passing argument to a function, copies
the actual value of an argument into the formal parameter of the
function. In this case, changed made to parameter inside the function
had no effect on argument.
7. 7
#include<stdio.h>
void swapvalue(int x,int y);
int main()
{
int a,b;
printf(“n Enter two numbers: ");
scanf("%d %d",&a,&b);
printf(“n Before Swapping a=%d, b=%d n",a,b);
swapvalue(a,b);
printf(“n After Swapping a=%d, b=%d n",a,b);
return 0;
}
void swapvalue(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
return;
}
Source Code:
Swap using 3rd
Variable
Function Definition
Function Declaration
Declaring Variable
Function Calling
Passing Variable
9. 9
Call by Reference
The Call by Reference method of passing arguments to a function copies
the address of an argument into the formal parameter. Inside the function,
the address is used to access the actual argument used in the call. This
means that changes made to be parameter affect the passed argument. To
pass the value by reference, argument pointers are passed to the functions
just like any other value. So, accordingly you need to declare the function
parameters as pointer types as in the following swap(), which exchanges
the values of the two integer variables pointed to by its arguments.
10. 10
#include<stdio.h>
void swapreference(int *x,int *y);
int main()
{
int a,b;
printf(“n Enter two numbers: ");
scanf("%d %d",&a,&b);
printf(“n Before Swapping a=%d, b=%d",a,b);
swapreference(&a,&b);
printf(“n After Swapping a=%d, b=%d",a,b);
return 0;
}
void swapreference(int *x, int *y)
{
int temp=0;
temp=*x;
*x=*y;
*y=temp;
return;
}
Source Code:
Swapping
address using
3rd Variable
Function Calling
Passing Address
Function Definition along
with defining Pointer
Function Declaration
Declaring Variable
12. › The function is called by
directly passing value of
variable as argument.
› We need to declare a
general variable as function
argument.
› Changes made in the formal
parameters will not be
reflected in the actual
parameters. It is because
formal and actual
parameters are stored in
different locations.
Call by Value v/s Call by Reference
› The function is called by
directly passing address of
variable as argument.
› We need to declare pointer
variable as function
argument.
› Changes made in the formal
parameters will be reflected
in the actual parameters. It is
because formal and actual
parameters are stored in
same locations.
12
Call by Value Call by Reference
13. 13
Conclusion
As the Conclusion , If we want to pass only the value of variable use ‘call by
value’ approach, and if you want to see the change in the original value of the
variable then use ‘call by reference’ approach.