The pointer can be used as arguments in functions. The arguments or parameters to the function are passed in two ways. i) Call by Value ii) Call by Reference
POINTERS AND FUNCTIONS
The
pointer can be used as arguments in functions. The arguments or parameters to
the function are passed in two ways.
i) Call by Value
ii) Call by Reference
The
process of passing the actual value of variables is known as "call by
value”. When a function is called in program, the values to the arguments in
the function are taken by the calling program, the value taken can be used
inside the function. Alteration to the value is not accepted inside the
function in calling program but change is locally available in the function.

Example 1: Write a
program to explain about call by value.
/* Program to explain
about call by value */
#include <stdio.h>
main()
{
int a=10, b=20;
printf("\n Before calling the function a&b is
%d,%d", a,b);
void change(a,b); /* function call */
printf("\n After calling a&b is %d,%d", a,b);
} /* main */
void change(x,y) /*
change is a function call with parameter */
int x,y;
{
x=x+5;
y=y+10;
}
printf ("In the function changes a&b is %d,
%d",x,y);
}
OUTPUT
Before call the
function a&b is 10,20
In the function
changes a&b is 15,30
After calling a&b
is 10,20
EXPLANATION:
The variable a,b are referred as x,y in the function. Inside the function the
values of the variables are added to constant value. But this changes will not
affect in the main program after function calling.
Example 2: Write a
program to swap (interchange) the two given variable.
/* Program to swap the two given variable *
#include <stdio.h>
main()
{
int a=10, b=20; /* Local
definitions */
printf ("\n. Before swap a=%d,b=%d",a,b);
void swap(a,b); /* swap is a function with parameters */
printf ("\n After the calling the swap function, a=
%d,b=%d", swap(a,b);
} /* main */
void swap(x,y)
int x,y;
{
x=x+y;
y=x‒y;
x=x‒y;
printf ("In swap function x= %d,y=%d",x,y);
} /* swap */
OUTPUT
Before swap, a=10,
b=20.
In swap function x=20,
y=10.
After calling the swap
function a=10, b=20.
EXPLANATION:
The main function control transfer to the user defined function swap(). Here
the variable a,b are referred as x,y. Inside the function block, the values are
exchanged one to another without using third variable, then print the values.
The main program prints the values of variable a and b after called the
function.
The
process of calling a function using pointers to pass the addresses of variables
is known as "call by reference". A function can be declared with
pointers as its arguments. Such functions are called by calling program with
the address of a variable as argument from it.
The
address of the variables is altered to the pointers and any changes made to the
value inside the function is automatically carried out in the location. This
change is accepted by the calling program.
Example 1: Write a
program to explain about call by reference.
/* Program to explain about call by reference */
#include <stdio.h>
main ()
{
int a=10; b=20;
printf ("\n
Before calling the function a&b is %d,%d",a,b);
void
change(&a,&b); /* change is a function call with parameters */
printf ("\n
After calling the function a&b is %d %d", a,b);
} /* main */
void change(x,y)
int x,y;
{
x=x+5;
y=y+10;
printf ("In
the function changes a&b is %d,%d", *x, *y);
} /* change */
OUTPUT
Before calling the
function a&b is 10,20..
In the function
changes a&b is 15,30.
After calling the
function a&b is 15,30.
EXPLANATION:
Prints values of variables a and b, then call function change() with address of
variables a, b are referred as x,y in the function. Inside the function, the
values are added to the constant variable and prints the values through pointer
variables x and y. The main function prints the value of a and b after function
calling.
Example 2: Write a
program to swap (interchange) the value of variables.
/* Program to swap the value of variables using call by
reference */
#include <stdio.h>
main()
{
int a=10, b=20;
printf ("\n
Before swap a=%d,b=%d", swap(a,b));
void
swap(&a,&b); /* swap is a
function call with parameters */
printf ("\n
After calling swap function a= %d,b=%d",a,b);
} /* main */
void swap(x,y)
int *x, *y; /* *x, *y is
a pointer variables */
{
x=x+y;
y=x‒y;
x=x‒y;
printf ("\n In
swap function x=%d,y=%d", *x, *y);
} /* swap */
OUTPUT
Before swap a=10,
b=20.
In swap function x=20,
y=10.
After calling swap
function a=20, y=10.
EXPLANATION:
Function called with address of variables a and b. The address of a,b are
referred as x,y. The variables x and y is declared as pointer variables. The
values of x and y are swapped without third variable and print the pointer
variable x and y. The main program prints the value of a and b after called the
swap() function.
Example 3: Write a
program to swap (interchange) the value of variables.
/* Program to swap the value of variables using call by
reference */
#include <stdio.h>
main()
{
/* Local
definitions */
int a,b;
/* Statements */
printf ("\n
Enter two number");
scanf("%d
%d", &a, &b);
printf("\n
Before Exchange a=%d,b=%d",a,b);
Exchange(&a,
&b); /* exchange is a function call with parameters */
printf ("\n
After Exchange a=%d,b=%d",a,b);
} /* main */
Exchange(m, n)
int *m, *n; /* *m, *n is a point variables */
{
/* Local
definitions */
int t;
/* Statements */
t=*m;
*m = *n;
*n=t;
return;
} /* exchange */
OUTPUT
Enter two number
10,20.
Before Exchange
a=10,b=20.
After Exchange a=20,b=10.
EXPLANATION:
Read values to a and b and print them. Call function exchange() with argument
as address of a and b. The variables a and b are referred as pointer variable m
and n, function body exchange the values of m and n using third variable t and return
the main program. In the main program, print a and b.

Call by Value
1.
Different memory locations are occupied by formal and actual arguments.
2.
Any alteration in the value of the arguments passed is local to the function
and is not accepted in the calling program.
3.
The usual method to call a function in which only the value of the variable is
passed as an argument.
4.
When a new location is created, it is very slow.
5.
There is no possibility of wrong data manipulation since the arguments are
directly used in an expression.
Call by Reference
1.
Same memory location occupied by formal and actual arguments, so there is a
saving of memory location.
2.
Any alteration in the value of the arguments passed is accepted in the calling
program.
3.
The address of the variable is passed as an argument.
4.
The existing memory location is used through its address, it is very fast.
There is a possibility of wrong data manipulation, since the addresses are used
in an expression. A good skill of programming is required here.
Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Pointers and Functions
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
English Essentials I
EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
தமிழர் மரபு - Heritage of Tamils
UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Calculus
MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I
PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Applied Chemistry I
CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Makerspace
ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming C
CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Computer Programming Python
CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation
Fundamentals of Electrical and Electronics Engineering
EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Mechanical Engineering
ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation
Introduction to Civil Engineering
CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing
CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation
Applied Physics I Laboratory
PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Applied Chemistry I Laboratory
CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming C Laboratory
CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Computer Programming Python Laboratory
CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation
Engineering Drawing
ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electronics and Electrical Engineering
EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation
Essentials of Computing - Laboratory
CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation