Académique Documents
Professionnel Documents
Culture Documents
FUNCTIONS
• A complete and independent
subprogram that can be developed
and tested successfully
What is a Function?
• The function takes a data from main ()
function and returns a value.
counter();
square();
message();
output();
Arguments List
The arguments are also called as parameters.
This tells what arguments the function needs
when it is called (and what their types are). The
different parameters are separated by commas.
return;
return (expression);
Ex: return;
return 3;
return n;
return ++a;
return (a*b);
Function Body
float add numbers (float n1, float n2)
{
return n1 + n2; /*legal*/
return 6; /*illegal, not the same data type*/
return 6.0; /*legal*/
}
Function Prototypes
For functions the declaration needs to be
done before the first call of the function.
There are two ways to do this. They are:
int message1 ( )
{
statement 1;
statement2;
statement 3;
}
Some facts about functions
4. Any function can be called from any other function
even main ( ) can be called from other functions. for e.g.
main ( )
{
message ( );
}
int message ( )
{
printf (“ \n Hello”);
main ( );
return 0
}
Some facts about functions
5. A function can be called any number
of times for eg.
message ( )
{
printf (“\n Hello”);
}
main ()
{
message ( );
message ( );
}
Some facts about functions
6. The order in which the functions are
defined in a program and the order in which
they get called need not necessarily be same
for e.g.
Some facts about functions
7. A function can call itself such a
process as called ‘recursion’.
8. A function can be called from other
function, but a function cannot be
defined in another function.
9. Any C program contains at least one
function
10. If a program contains only one
function, it must be main ( )
Types of Functions
1) A function with no arguments and
no return value
2) A function with no arguments and
return a value
3) A function with an argument or
arguments and returning no value
4) A function with arguments and
returning a value.
A Function With No Arguments And
No Return Value
A Function With No Arguments And
No Return Value
main( )
{
message ( );
}
void message( )
{
printf(“\n WELCOME”);
}
A Function with No Arguments and
Returns a Value
A function which does not get value from the calling
function but it can return a value to calling program.
This can be represented as given below.
A Function with Arguments and
Returns No Value
A Function with Arguments and
Returns No Value
#include
main()
{
int a,b;
printf(“Enter the two numbers”);
scanf(“%d%d”,&a,&b);
largest(a,b)
}
fun (a,b)
int a,b;
{
int y,z;
y=a+b;
z=a-b;
return y;
return z; /* Two return statements for one single function called is invalid * /
}
A Function with Arguments and
Returning a Value
A Function with Arguments and
Returning a Value
#include<stdio.h>
float findaverage(float a, float b); /*Function prototype*/
main()
{
float a=5,b=15,result;
result=findaverage(a,b);
printf("average=%f \ n",result); /*Function prototype*/
}