14.8 – Pass by reference – Programs to practice

by subbu on January 21, 2014

Pass by reference – Programs to practice

Pass by reference, pass by address are same

I forgot to clarify this in the previous sessions. Some authors says passing address to the functions is called pass by reference and others say pass by address. Make sure that both are same, believe me there is no difference among them.

In and out parameters in C language:

We know that any function is capable to accept any number of arguments but, can return a single value. It is possible to send back any number of values to the calling function from the called by function through pointers of actual arguments in the called by function that is through pass by reference.

Here sending values to the called by function are called in type parameters and sending the values back to the calling function through dereferencing is called out parameters.

In this session we will see some example programs to understand pass by reference

Specification: Accept the radios of a circle and print the area, circumference

#define PI 3.14
void circle(short,float*,float*);
int main()
short rad;
float area,cir;
printf("Enter the radios of circle:");
printf("Area of circle %f",area);
printf("\nCircumference of circle %f",cir);
return 0;
void circle(short r,float *a,float *c)

Enter the radios of circle:17
Area of circle 907.460022
Circumference of circle 106.760002

in and out parameters

Here radios (rad) is send as pass by value and area, cir are send as pass by reference to the function circle() by sending their addresses.
We calculate the area, circumference of circle using formal argument r and assign the values to actual arguments (area, cir) from the circle() through their pointers using dereferencing (*a, *c)
Here r is in type parameter and area, cir are out type parameters from the function circle().

Specification: Accept any integer and print the minimum and maximum digits of the given number using the function limits()

void limits(short,short*,short*);
int main()
short n,min,max;
printf("Enter any integer:");
printf("The minimum digit %d",min);
printf("\nThe maximum digit %d",max);
return 0;
void limits(short n,short *mn,short *mx)

Enter any integer:25413
The minimum digit 1
The maximum digit 5

Here an integer number n is sending as pass by value to the sub function limits()
Addresses of min and max are sending to the function to get back minimum and maximum digits.
Formal arguments mn and mx are pointers to min and max, assigning minimum and maximum digits using dereferencing

Specification: Accept n elements into an array, print how many 0’s, even and odd elements are existed using a function process()

void process(short[],short,short*,short*,short*);
int main()
short a[50],n,nz=0,ne=0,no=0,i;
printf("How many elements?");
printf("Enter %d elements:\n",n);
printf("Number of zeros %d",nz);
printf("\nNumber of even numbers %d",ne);
printf("\nNumber of odd numbers  %d",no);
return 0;
void process(short x[50],short n,short *z,short *e,short *o)
 short i;
  else if(x[i]%2==0)


How many elements?7
Enter 7 elements:
12 35 77 14 0 11 0
Number of zeros 2
Number of even numbers 2
Number of odd numbers  3

Here pointers to nz (number of zeros), ne (number of even numbers) and no (number of odd numbers) are z, e and o
Using dereferencing we are sending back number of zeros, even numbers and odd numbers to the calling function

Specification: Accept the salary of an employee and print the hra, da and gross using the function calc_sal(). Say hra is 12% of salary and da is 13% of salary

void calc_sal(int,float*,float*);
int main()
int sal;
float hra,da,gross;
printf("Enter the salary:");
printf("Salary details of employee:");
printf("\nHouse Rent Allowance %f",hra);
printf("\nDearness Allowance  %f",da);
printf("\nGross Salary         %f",sal+hra+da);
return 0;
void calc_sal(int s,float *h,float *d)

Enter the salary:3500
Salary details of employee:
House Rent Allowance 420.000000
Dearness Allowance  455.000000
Gross Salary         4375.000000

Previous post:

Next post: