Module-3 Functions
Module-3 Functions
1. INTRODUCTION
Every C program should consist of one or more functions. Among these functions main(
) function is compulsory. All programs start execution from main( ) function.
Functions are independent program modules that are designed to carry out a particular task. In
functions we have two types:
1. Built-in (Library) functions
2. User defined functions
Built-in functions are C language functions already available with C compliers and can be
used by any programmers. Example: printf( ),scanf( )
User-defined functions are written by programmers to serve their own purpose and are not
readily available.
1
Following block diagram explains how functions are useful:Instead of writing entire program
(to add and subtract) within main( ) function we can write independent modules add( ) and
subtract( ) as shown:
main() Subtract ( )
) completes the work
main( ) calls add( )
and returns back to
main( )
Function
output
inputs Black- box
Note: As shown in the above diagram, function can accept any number of inputs but one
value as output.
2. MODULAR PROGRAMMING
2
3. ELEMENTS OF USER-DEFINED FUNCTIONS
There are three elements that are related to functions:
i. Function definition
ii. Function call
iii. Function declaration/Function prototype
DEFINITION OF FUNCTIONS
All the above six elements are grouped into two parts. Namely:
Function header (First three elements)
Function body (Second three elements)
Syntax:
function_type function_name(parameter list)
{
local variable declaration; executable
statement1;
executable statement2;
......
......
return statement;
}
3
The first line function_type function_name(parameter list) is known as the function
header and the statements within opening and closing braces is known as the function body.
i. Function Header: It consists of three parts: Function type, function name and formal
parameter list. The semicolon is not present at the end of header.
Function type:
The function type specifies the type of value that the function is expected to return to
the program calling the function.
If the return type is not specifies, then it is assumed as int.
If function is not returning anything, then it is void.
Function name: The function name is any valid C identifier and must follow same rules as
of other variable.
Formal Parameter List:The parameter list declares the variables that will receive the data
sent by the calling program which serves as input and known as Formal Parameter List.
ii. Function Body: The function body contains the declarations and statements necessary
for performing the required task. The body is enclosed in braces, contains three parts:
1. Local declarations that specify the variables needed by the function.
2. Function statements that perform the task of the function.
3. A return statement that returns the value evaluated by the function.
FUNCTION DECLARATION
Syntax:function_type function_name(parameter_list);
4
4. CATEGORIES OF FUNCTIONS
User defined
functions
Functions return
nothing Functions with return
value
(void functions)
Functions without
void functions with parameters and
parameters with return values
When a function has no arguments, it does not receive any data from the calling
function.
Similarly, when it does not return a value, the calling function does not receive any
data from the called function.
5
void display(void);
int main( ) 1
{ 2 Parts of user defined function marked in the
example given are:
display( ); 3 1. function prototype
2. calling function
3. called function/function call
} 4. function definition
4a
a. function header
void display(void ) i. return type
{ ii. function name
printf(“Welcome to the C World”); iii. parameters if any
return; b. function body
}
4b
6
2. Void Functions with parameters – Arguments but no return values
The calling function accepts the input, checks its validity and pass it on to the called
function.
The called function does not return any values back to calling function.
The actual and formal arguments should match in number, type and order as shown in
Figure 3.
Figure 5: Arguments matching between the function call and the called function
7
void show(int);
int main( ) 2 1
Description of user defined function marked in
{
the example given is:
int num=5;
show(num); 3 1. function prototype of show( ) has
an integer parameter ‘int’
2. calling function main ( )
}/* end of main */
3. actual parameter ‘num’ is
passed to function show( )
void show(int no) 4. formal parameter ‘no’contains
{ value 5 copied from ‘num’ and
printf(“%d”, no); 4 displays it on screen
return;
}
In this example a photocopy of value stored in ‘num’ (i.e. 5) is made in ‘no’ and finally
displayed on screen. Next let us discuss functions with parameters and return values.
8
3. Functions with parameters and return values – Arguments with return
values
The calling function sends the data to called function and also accepts the return
values from called function.
Actual Parameters: When a function is called, the values that are passed in the call
are called actual parameters.
Formal Parameters:The values which are received by the function, and are
assigned to formal parameters.
9
4. Functions without parameters and with return values – No arguments but
returns values
The calling function does not send any data to called function and but, accepts the
return values from called function.
Global Variables: These are declared outside any function, and they can be
accessed on any function in the program.
Local Variables: These are declared inside a function, and can be used only
inside that function.
Note: Function call can be an expression like: “square=SquareMaker( )” only in functions with
return values. Void function call cannot be an expression.
10
5. Inter-Function Communication
Two functions can communicate with each other by two methods:
void main()
void swap( int x, int y )
{
{
int a=5, b=10;
int temp;
swap(a, b);
temp=x;
printf(“%d %d”, a,b)
x=y;
}
y=temp;
temp=x /* copies 5 to
11
2. By Passing parameters as address (Pass by address)
12
6. NESTING OF FUNCTIONS
A function within a function is called as nesting of functions.
float checkno(float );
float div(float,float);
main( )
{
float a,b,res;
printf(“enter two numbers\n”);
scanf(“%f%f”),&a,&b);
div(a,b);
}
float div(float a, float b);
{
if(checkno(b))
printf(“the result is %f”, a/b);
else
printf(“division not possible”);
}
float checkno(float b);
{
if(b!=0)
return 0;
else
return 1;
}
13
7. PASSING ARRAYS TO FUNCTIONS
In large programs that use functions we can pass Arrays as parameters. Two ways of passing
arrays to functions are:
1. Pass individual elements of array as parameter
2. Pass complete array as parameter
Let us take an example program that uses a function square( ) to calculate square of numbers
stored in an array. In this program each array element is passed one by one.
This diagram clearly illustrates how each individual element of array num[ ] is passed to
function square through parameter no.
14
Module 4 C Programming for Problem Solving
3 4 8 9 10
0 1 2 3 4 num[3]=9 no
num[1]=4 copied no
num[0]=3 copied to no
num[2]=8 no
Next program illustrates how to pass an entire array to a function average( ) and calculate
average marks. First sum of all the elements of array marks (i.e.35+65+75+95+85) is
calculated and average is stored in a variable avg. value in avg variable is returned to main( )
and stored in avg1
Output:
average=71.000000
In the above example each value of array marks[ ] is copied to array scores[ ] as shown
below:
marks[0]=35 copied to scores[0]
9. RECURSION
14
Module 4 C Programming for Problem Solving
Limitations of Recursion
Recursive solutions may involve extensive overhead because they use function calls.
Each time you make a call, you use up some of your memory allocation. If
recursion is deep, then you may run out of memory.
PROGRAMS
1. Write a C program to find a factorial of number using recursion.
#include<stdio.h> int
fact(int);
void main( )
{
int n,res;
printf(“enter the number to find its factorial\n”); scanf(“%d”,&n);
res=fact(n);
printf(“factorial of %d=%d”,n,res); getch();
}
int fact(int n)
{
if(n==0) return
1;
else
return (n*fact(n-1));
15
Module 4 C Programming for Problem Solving