When a function is called, the calling function may have to pass some values to the called function. We have been doing this in the programming examples given so far.
PASSING
PARAMETERS TO FUNCTIONS
When
a function is called, the calling function may have to pass some values to the
called function. We have been doing this in the programming examples given so
far. We will now learn the technicalities involved in passing
arguments/parameters to the called function.
There
are two ways in which arguments or parameters can be passed to the called
function. They include:
•
Call by value in which values of
variables are passed by the calling function to the called function. The
programs that we have written so far call functions using call-by- value method
of passing parameters.nut boltso sdt mi
•
Call by reference in which address
of variables are passed by the calling function to the called function.
Till
now, we had been calling functions and passing arguments to them using
call-by-value method. In the call- by-value method, the called function creates
new variables to store the value of the arguments passed to it. Therefore, the
called function uses a copy of the actual arguments to perform its intended
task.
Programming Tip:
It
is legal to have multiple return statements in C.
If
the called function is supposed to modify the value of the parameters passed to
it, then the change will be reflected only in the called function. In the
calling function no change will be made to the value of the variables. This is
because all the changes were made to the copy of the variables and not to the
actual variables.
To
understand this concept, consider the code given below. The add() function accepts an integer
variable num and adds 10 to it. In
the calling function, the value of num = 2. In add (), the value of num
is modified to 12 but in the calling function the change is not reflected.
#include <stdio.h>
void add(int n);
int main()
{
int num = 2;
printf("\n The value of num
before calling the function = %d", num);
add (num);
printf("\n The value of num
after calling the function = %d", num);
return 0;
}
void add(int n)
{
n = n + 10;
printf("\n The value of num in
the called function %d", n);
}
Output
The value of num before calling the
function = 2
The value of num in the called
function = 12
The value of num after calling the
function = 2
Since
the called function uses a copy of num, the value of num in the calling
function remains untouched. This concept can be more clearly understood from
Figure 4.5.
In
the above program, the called function could not directly modify the value of
the argument that was passed to it. In case the value has to be changed, then
the programmer may use the return statement. This is shown in the following
code.
#include <stdio.h>
int add (int n);
int main()
{
int num = 2;
printf("\n The value of num
before calling the function %d", num);
num = add (num);
printf("\n The value of num
after calling the function = %d", num);
return 0;
}
int add (int n)
{
n = n + 10;
printf("\n The value of num in
the called function = %d", n);
return n;
}
Output
The value of num before calling the
function = 2
The value of num in the called
function = 12
The value of num after calling the
function = 12
The
following points are to be noted while passing arguments to a function using
the call-by-value method.
•
When arguments are passed by value, the called function creates new variables
of the same data type as the arguments passed to it.
•
The values of the arguments passed by the function are copied into the newly
created variables.
•
Arguments are called by value when the called function does not need to modify
the values of the original variables in the calling function.
•
Values of the variables in the calling function remain unaffected when the
arguments are passed using call by value technique.
Therefore,
call-by-value method of passing arguments to a function must be used only in
two cases:
•
When the called function does not need to modify the value of the actual
parameter. It simply uses the value of the parameter to perform its task.
•
When you want that the called function should only temporarily modify the value
of the variables and not permanently. So although the called function may
modify the value of the variables, these variables remain unchanged in the
calling function.
Programming Tip:
Using
call by value method of passing values is preferred to avoid inadvertent
changes to variables of the calling function in the called function.
Pros and Cons
The
biggest advantage of using the call-by-value technique to pass arguments to the
called function is that arguments can be variables (e.g., x), literals (e.g.,
6), or expressions (e.g., x+1).
The
disadvantage is that copying data consumes additional storage space. In
addition, it can take a lot of time to copy, thereby resulting in performance
penalty, especially if the function is called many times.
When
the calling function passes arguments to the called function using
call-by-value method, the only way to return the modified value of the argument
to the caller is explicitly using the return statement. The better option when
a function wants to modify the value of the argument is to pass arguments using
call-by-reference technique. In call by reference, we declare the function
parameters as references rather than normal variables. When this is done any
changes made by the function to the arguments it receives are visible in the
calling function.
To
indicate that an argument is passed using call by reference, an asterisk (*) is
placed after the type in the parameter list. This way, changes made to the
parameter in the called function will then be reflected in the calling
function.
Hence,
in call-by-reference method, a function receives an implicit reference to the
argument, rather than a copy of its value. Therefore, the function can modify
the value of the variable and that change will be reflected in the calling
function as well. The following program uses this concept. To understand this
concept, consider the code given below.
#include <stdio.h>
void add (int *n);
int main()
{
int num = 2;
printf("\n The value of num
before calling the function = %d",
num);
add (&num);
printf("\n The value of num
after calling the function = %d", num);
return 0;
}
void add (int *n)
{
*n = *n + 10;
printf("\n The value of num in
the called function = %d", *n);
}
Output
The value of num before calling the
function = 2
The value of num in the called
function = 12
The value of num after calling the
function = 12
Advantages
The
advantages of using the call-by-reference technique of passing arguments are as
follows:
•
Since arguments are not copied into new variables, it provides greater time and
space efficiency.
•
The called function can change the value of the argument and the change is
reflected in the calling function.
•
A return statement can return only
one value. In case we need to return multiple values, pass those arguments by
reference.
Disadvantages
However,
the side-effect of using this technique is that when an argument is passed
using call by address, it becomes difficult to tell whether that argument is
meant for input, output, or both.
Now
let us write a few programs that use both the call- by-value and the
call-by-reference mechanisms.
2. Write a function
to swap the value of two variables.
#include <stdio.h>
void swap_call_by_val (int, int);
void swap_call_by_ref (int *, int
*);
int main()
{
int a=1, b=2, c=3, d=4;
printf("\n In main(), a = %d
and b = %d", a, b);
swap_call_by_val (a, b);
printf("\n In main(), a = %d
and b = %d",a, b);
printf("\n\n In main(), c = %d
and d = %d", c, d);
swap_call_by_ref (&c, &d);
// address of the variables is
passed
printf("\n In main(), c = %d
and d = %d", c, d);
return 0;
}
Void swap_call_by_val (int a, int
b)
{
int temp;
temp = a ;
a = b;
b = temp;
printf("\n In function (Call
By Value Method) a = %d and b = %d", a,b);
}
void swap_call_by_ref (int *c, int
*d)
{
int temp;
temp = *C;
// *operator used to refer to the
value
*C = *d;
*d = temp;
printf("\n In function (Call
By Reference Method) c = %d and d = %d", *c, *d);
}
Output
In main(), a = 1 and b= 2
In function (Call By Value Method)
a = 2 and b = 1
In main(), a = 1 and b = 2
In main(), c = 3 and d = 4
In function (Call By Reference
Method) c = 4 and d = 3
In main(), c = 4 and d = 3
3. Write a program to
find biggest of three integers using functions.
#include <stdio.h>
int greater (int a, int b, int c);
int main()
{
int numl, num2, num3, large;
printf("\n Enter the first
number: ");
scanf("%d", &num1);
printf("\n Enter the second
number: ");
scanf("%d", &num2);
printf("\n Enter the third
number: ");
scanf("%d", &num3);
large = greater (num1, num2, num3);
printf("\n Largest number
%d", large);
return 0;
}
int greater (int a, int b, int c)
{
if (a>b && a>c)
return a;
if (b>a && b>c)
return b;
else
return c;
}
Output
Enter the first number: 45
Enter the second number: 23
Enter the third number: 34
Largest number = 45
4. Write a program to
calculate area of a circle using function.
#include <stdio.h>
float cal area (float r);
int main()
{
float area, radius;
printf("\n Enter the radius of
the circle: ");
scanf("%f", &radius);
area = cal_area (radius);
printf("\n Area of the circle
with radius %f = %f", radius, area);
return 0;
}
float cal_area (float radius)
{
return (3.14 * radius * radius);
}
Output
Enter the radius of the circle: 7
Area of the circle with radius 7 =
153.83
5. Write a program to
convert time to minutes.
#include <stdio.h>
#include <conio.h>
int convert_time_in_mins (int hrs,
int minutes);
int main()
{
int hrs, minutes, total_mins;
printf("\n Enter hours and
minutes: ");
scanf("%d %d", &hrs,
&minutes);
total_mins = convert_time in mins
(hrs, minutes);
printf("\n Total minutes = %d", total_mins);
getch();
return 0;
}
int convert_time_in_mins (int hrs,
int mins)
{
mins = hrs*60+ mins;
return mins;
}
Output
Enter hours and minutes: 4 30
Total minutes = 270
6. Write a program to
calculate P(n/r).
#include <stdio.h>
#include <conio.h>
int Fact (int);
int main()
{
int n, r;
float result;
clrscr();
printf("\n Enter the value of
n: ");
scanf("%d", &n);
printf("\n Enter the value of
r: ");
scanf("%d", &r);
result = (float) Fact (n) /Fact
(r);
printf("\n P(n/r): P(%d) /
(%d) = %.2f", n, r, result);
getch();
return 0;
}
int Fact (int num)
{
int f=1, i;
for (i=num; i>=1; i--)
f = f*i;
return f;
}
Output
Enter the value of n: 4
Enter the value of r: 2
P(n/r): P(4) / (2) = 12.00
7. Write a program to
calculate C(n/r).
#include <stdio.h>
#include <conio.h>
int Fact (int);
int main()
{
int n, r;
float result;
clrscr();
printf("\n Enter the value of
n: ");
scanf("%d", &n);
printf("\n Enter the value of
r: ");
scanf("%d", &r);
result = (float) Fact (n) / (Fact
(r) *Fact (n-r));
printf("\n C(n/r) : C(%d/%d) =
%.2f", n, r, result);
getch();
return 0;
}
int Fact (int num)
{
int f=1, i;
for (i=num; i>=1; i--)
f = f*i;
return f;
}
Output
Enter the value of n: 4
Enter the value of r: 2
C(n/r): C(4)/(2) 6.00
8. Write a program to
sum the series-1/1! + 1/2! + 1/3!...........+ 1/n!.
#include <stdio.h>
#include <conio.h>
int Fact (int);
int main()
{
int n, f, i;
float result=0.0;
clrscr();
printf("\n Enter the value of
n: ");
scanf("%d", &n);
for (i=1;i<=n;i++)
{
f=Fact (i);
result += 1/(float) f;
}
printf("\n The sum of the
series 1/1! + 1/2! + 1/3!... = %.1f", result);
getch(),
return 0;
}
int Fact (int num)
{
int f=1, i;
for (i=num; i>=1;i--)
f = f*i;
return f;
}
Output
Enter the value of n: 2
The sum of the series 1/1! + 1/2! +
1/3!...= 1.5
9. Write a program to
sum the series-1/1! + 4/2! + 27/3! +…….
#include <stdio.h>
#include <conio.h>
#include <math.h>
int Fact (int);
int main()
{
int n, i, NUM, DENO;
float sum=0.0;
clrscr();
printf("\n Enter the value of
n");
scanf("%d", &n);
for (i=1;i<=n;i++)
{
NUM = pow(i,i);
DENO = Fact (i);
sum += (float) NUM/DENO;
}
printf("\n 1/1! + 4/2! + 27/3!
+……%.2f", sum);
getch();
return 0;
}
int Fact (int n)
}
int f=1, i;
for (i=n;i>=1; i--)
f=f*i;
goo!
return f;
}
Output
Enter the value of n: 3
1/11 + 4/2! + 27/3! + ....= 7.50
Programming in C: Unit III (a): Functions : Tag: : with Example C Programs - Passing Parameters to Functions
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation