Sei sulla pagina 1di 13

Function: A functions is an independent module that will be

called to do a specific task.


C functions are of two types.
They are
1. system defined\Library functions.
2. User defined functions.
1. System defined functions:
These functions pre-defined functions which are available
in c library.
Examples : clrscr( );
getch( );
printf( );
scanf( );
gets( );
puts( );
sqrt( );
pow( );
2. Use- defined functions:
These function definition should be defined by the user or
programmer.
users will write these functions to perform a specific task
based on the problem.
For user defined functions we need to specify three parts
They are:
1. Function declaration
2. Function definition
3. Function call
Function definition/function prototype:
function prototype will specify the following elements
(i) Return type
(ii)function name
(iii)Number of parameters
(iv)datatype of parameters
function prototype statement should be terminated with
a semicolon.
Function definition:
In function definition we will specify the following
(i) function header
(ii) Function body
In function body we can write declaration executable
statements.
Function call:
While calling a function we have to pass the number
of parameters\values to the function.
The parameters that appear in the functions call are called
actual parameters .
The parameters which appear in function header are
called formal parameters
we can call a function in two ways.
• call by value
•call by reference

Call by reference:
If function parameters are pointer type,then While calling that
function we have to pass the addresses of the data . this way of
calling a function is called call by reference.
Function categories:

1.Function without parameters & no return value


2.Function with parameters & no return value
3.Function without parameters & return value
4.function with parameters & return value
Function without parameters & no return value:
#include<stdio.h>
#include<conio.h>
void main( )
{
clrscr( );
add( );
getch( );
}
void add(void)
{
int a,b;
printf(“Enter values of a,b \n”);
scanf(“%d%d”,&a,&b);
printf(“sum=%d”,a+b);
}
Function with parameters & no return value:
#include<stdio.h>
#include<conio.h>
void add (int, int);
void main( )
{
int x,y;
clrscr( );
printf(“ Enter two integers \n”);
scanf(“%d%d”,&x,&y);
add(x,y);
getch( );
}
void add( int a, int b)
{
printf(“sum=%d”,a+b);
}
Function without parameter & with return value:
#include<stdio.h>
#include<conio.h>
void main ( )
{
int c;
clrscr( );
c = add( );
printf(“sum = %d”,c);
getch( );
}
int add( )
{
int a,b;
printf(“Enter values of a,b);
return(a+b);
}
Function with parameters & with return value:
#include<stdio.h>
#include<conio.h>
int add(int, int);
void main( )
{
int x,y,z;
clrscr( );
printf(“Enter x,y values \n”);
scanf(“%d%d”,&x,&y);
z=add(x,y);
getch( );
}
int add(int a, int b)
{
return(a+b);
}
Inter function communication
Functions will communicate with one another, this is called
inter function communication.
While communicating one function with another function by
passing data\parameters and other functions may return values
to the calling function.
The function in which another function call appears, that
function is called calling function the other one is called
function.
Function communication will takes place in the following
forms.
1. Download communication
2. Upwards communication
3. Bidirectional communication
Download communication:

calling function
fun1( )

fun 2( ) C called function

In download communication, calling function will pass data


to the called function whereas called function will not return
any value to the caller.
Upward communication:

fun 1 fun1( )( ) calling function

fun 2( ) called function

In upwards communication, data flow will takes place from


called function to the calling function.
Bidirectional communication:

fun1( )(
calling function

fun 2( )
called function

Here data will flow in both directions, that is from


calling function to the called function and
called function to the calling function

Potrebbero piacerti anche