Functions
Functions
It is independent which
can perform its task
without interference
A function is named
from or interfering with
other parts of the
program.
A function is defined
Every C program has at using its predefined
least one function, syntax, and are called
which is main() from elsewhere in your
program.
Why Functions?
Improves the
readability of code
Debugging of code
Avoid duplicating
is easier, errors are
codes
easy to be traced.
Syntax:
return_type function_name( arg-typ name-1,...,arg-typ name-n);
Example:
int hex(int val);
int max(int num1, int num2);
When a program
By creating a C To use a function, calls a function, the
function, we give a we will have to call program control is
definition of what that function to transferred to the
the function has to perform the defined called function
do. task. which performs a
defined task
#include <stdio.h>
int add(int a, int b); //function declaration
int main()
{ //main function definition
int a = 5, b = 10;
int sum;
printf("The value of a and b : %d %d ", a, b);
The value of a
sum = add(a, b); //function call and b : 5 10
printf("\nsum = %d ", sum); sum = 15
}
//function definition
int add(int a, int b)
{
int c;
c = a + b;
return c; //returns a integer value to the calling function
}
Classification of user defined functions into four
types, according to parameters and return value
The calling function will not send parameters to the called function
and called function will not pass the return value to the calling
function.
#include <stdio.h>
void add(); //function declaration
int main()
{ //main function definition
add(); //function call without passing arguments
}
//function definition
The value of a and b :
void add() 5 10
{ int a = 5, b = 10; sum = 15
int c;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
printf(" \nsum : %d ", c); //no return values to the calling function
}
Functions With Arguments Without Return Values
The calling function will pass parameters to the called function but
called function will not pass the return value to the calling function.
#include <stdio.h>
void add(int,int); //function declaration
int main()
{ //main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
add(a, b); //function call with passing arguments to called function }
// function definition
The value of a and b :
void add(int a, int b) 5 10
{ sum = 15
int c;
c = a + b;
printf(" \nsum : %d ", c); //no return values to the calling function
main
}
Functions Without Arguments With Return Values
The calling function will not pass parameters to the called function
but called function will pass the return value to the calling function.
#include <stdio.h>
int add(); //function declaration
int main()
{ //main function definition
int sum;
sum = add(); //function call without passing arguments to called
function
printf(" \nsum = %d ", sum);
}
The value of a and b :
// function definition 5 10
int add() sum = 15
{ int c;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
return c; //passing return values to the calling function main }
Functions With Arguments With Return Values
The calling function will pass parameters to the called function and
called function also pass the return value to the calling function
#include <stdio.h>
int add(int, int); //function declaration
int main()
{ //main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
sum = add( a, b); //function call with passing arguments
printf(" \nsum = %d ", sum);
}
// function definition The value of a and b :
5 10
int add(int a, int b)
sum = 15
{ int c;
c = a + b;
return c; //passing return values to the calling function main
}
Function Parameters
There are two types of function parameters:
Formal parameters:
Appear in a declaration or a definition of a function.
Actual parameters:
Appear in a call to the function.
Examples:
int f(int x); here x is a formal parameter
Related Function
The actual parameters are passed by The formal parameters are in the called
the calling function. function.
Data Types
4. Thus actual values of a and b remain 4. Thus actual values of a and b get
unchanged even after exchanging the changed after exchanging values of x
values of x and y. and y.
7. A copy of the original variables are 7. No copy of the variables are created
created in memory stack. on the memory stack.