CPPS Mod
CPPS Mod
MODULE 4:
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.
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 ( )
main( ) calls add( ) completes the work
and returns back to
main( )
Note: As shown in the above diagram, function can accept any number of inputs but can retu
one value as output.
2. MODULAR PROGRAMMING
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;
}
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. 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.
1. function prototype
display( ); 3 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
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, ty[e and order as
shown in Figure 3.
Figure 5: Arguments matching between the function call and the called function
void show(int);
1
int main( ) 2 Description of user defined function marked in the
{ example given is:
int num=5; 1. function prototype of show( ) has an
show(num); 3 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 displays it on
{
4 screen
printf(“%d”, no);
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
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.
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.
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;
}
y=temp /* copies 5 to y */
Value in b=10 Copied to y Thus x and y values got swapped, but problem is a and b
values remain unchanged!
} *y=temp;
}
Address of is Copied to
temp=*x /* copies 5 to temp */
a=say(1002) x
*x=*y /*copies 10 to *x (i.e. to a) */
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;
}
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.
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
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));
1. Explain function call, function definition and function prototype with examples to
each.
2. What are actual parameters and formal parameters? Illustrate with example.
3. What is recursion? WACP to compute the factorial of a given number ‘n’ using
recursion.
4. Explain recursion with example.
5. Define the following:
i. Actual parameter
ii. Formal Parameter
iii. Global variable