Module3-function
Module3-function
Functions
3.12 Introduction
C enables its programmers to break up a program into segments commonly known as functions,
each of which can be written more or less independently of the others.
Every function in the program is supposed to perform a well-defined task.
Therefore, the program code of one function is completely insulated from the other functions.
Definition
“The set of instructions that performs some specific, well-defined task is called as a Function.”
Or
“Function is a small program or program segment that carryout some specific well-defined
tasks”.
Fig. 1.9 explains how the main() function calls another function to perform a well-defined task.
In the figure, we can see that main() calls a function named func1(). Therefore, main() is known as the
calling function and func1() is known as the called function.
The moment the compiler encounters a function call, the control jumps to the statements that are a
part of the called function.
After the called function is executed, the control is returned to the calling program.
1. Call by Value
In this 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.
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 are made to the copy of the
variables and not to the actual variables.
void main()
{
int a,b, res;
printf(“Enter the values of a and b:”);
scanf(“%d%d”,&a,&b);
res = add(a,b);
printf(“result =%d\n”, res);
}
2. Call by Reference
When the calling function passes arguments to the called function using the call-by-value
method, the only way to return the modified value of the argument to the caller is explicitly
using the return statement. A better option is to pass arguments using the call-by-reference
technique.
In this method, 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 received are also
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. Hence, in the 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.
Example:
1. Write a C program to add two numbers using call by reference.
#include<stdio.h>
void main()
{
int a,b, res; Output:
printf(“Enter the values of a and b:”); Enter the values of a and b: 4 5
scanf(“%d%d”,&a,&b); result =9
res = add(&a,&b);
printf(“result =%d\n”, res);
}
Output:
Enter the values of a and b: 10 20
Before swapping: a=10 b=20
After swapping: a=20 b=10
Advantages
Since arguments are not copied into the new variables, it provides greater time and space efficiency.
The function can change the value of the argument and the change is reflected in the calling
function.
A function can return only one value. In case we need to return multiple values, we can pass those
arguments by reference, so that the modified values are visible in the calling function.
Disadvantages
However, the drawback of using this technique is that if inadvertent changes are caused to variables
in called function then these changes would be reflected in calling function as original values
would have been overwritten.
Place of Declaration: The Global variables are declared outside all the functions including main(). It
is always recommended to declare them on top of the program code.
Name conflict: If we have a variable declared in a function that has same name as that of the global
variable, then the function will use the local variable declared within it and ignore the global variable.
Consider the following program,
#include <stdio.h >
int x = 10;
void print();
void main()
{
printf(“\n The value of x in the main() = %d”, x);
int x = 2;
printf(“\n The value of local variable x in the main() = %d", x);
print();
}
void print()
{
printf(“\n The value of x in the print() = %d”, x);
}
Output:
The value of x in the main() = 10
The value of local variable x in the main()= 2
The value of x in the print () = 10
3.20 Recursion
“The process in which a function calls itself again and again is called as Recursion”.
A recursive function is defined as a function that calls itself to solve a smaller version of its task
until a final call is made which does not require a call to itself.
Every recursive function must have a base case and a recursive case. For the factorial function,
Base case is when n = 1, because if n = 1, the result will be 1 as 1! = 1.
Recursive case of the factorial function will call itself but with a smaller value of n, this case can
be given as:
factorial(n) = n × factorial (n–1)
Example:
1. Write a C program to calculate factorial of a given number.
#include<stdio.h>
int factorial(int n)
{
if(n==1)
return 1;
else
return (n*fact(n-1));
}
void main()
{
int x, y, res; Output:
printf("\n Enter the two numbers: "); Enter the two numbers: 8 12
scanf("%d %d", &x, &y); GCD = 4
res = GCD(x, y);
printf("\n GCD = %d", x, y, res);
}
inprotected.com