Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

C programming

presentation

FUNCTIONS
Presented by:Nitesh M.
Chahande
F.E.
(Electronics)
Roll No. 26

What is a Function ?
Function is a self contained block of
statements that perform a coherent
task of some kind.
Every C program can be a thought of
the collection of functions.
main( ) is also a function.

Types of Functions.
Library functions: These are the in in-C built functions of
library.
These are already defined in header files.
e.g. printf( ); is a function which is used
to
print at output.
It is defined in stdio.h file .

Types of Functions.
User defined functions.
Programmer can create their own
function in C to perform specific task.

e.g.
#include< stdio.h h>
Main()
{
message();
getch();
return();
}
message()
{
printf(\n Hello);
}

Declaration of many
#include< stdio.hfunctions.
>
main( )
{
printf( I am in main );
engg( );
agri( );
getch();
return;
}
engg( )
{
printf(I am in engineering );
}
agri( )
{
printf(I am in agri );
}

Some conclusions.
Any C Program must contain at least one
function.
There is no limit on the number of functions
present in a C program.
Each function in a program is called in the
sequence specified by functions call in main( ).
If program contains only one function it must
be main( ).
After each function has done its thing, control
returns to main( ).
When main( ) run out of function calls program
ends.

Summarization.
C Program is a collection of one or more function
A function gets called when its name is followed by a semicolon.
e.g.
main( )
{
fun( );
getch();
return;
}
fun( )
{
statement1;
statement2;
statement3;
}

Any function can be called by any other function.


e.g.
main( )
{
printf(I am in main. );
fun();
getch();
return;
}
fun()
{
printf(I am in fun. );
main( );
}
This will run in infinity.

A function can call itself, it is called as a


recursion.
e.g.
main ( )
{
printf(I am in main );
main( );
}

A function can be called from another


function but can not be defined in another
function. It should be defined outside of
the main

Why use functions ?


Writing functions avoids rewriting of
the same code again and again in
the program
Using a function it becomes easier to
write program to keep track of what
they are doing.

Passing values to
functions.
The values that we pass to the function called as
function arguments.
e.g.
main( )
{
int i=10, j=12, k=20;
calsum(i,j,k);
}
calsum(int x, int y, int z)
{
printf( Sum is : %d ,(x+y+z ));
}

Returning a value.
The keyword return is used to
return value from function which
is present in the parenthesis of
return.
On executing return statement it
immediately transfers control
back to the main program.

e.g.
main( )
{
int a = 30, j = 14;
int k;
k = addin(a , j);
printf(%d, k);
}
int addin(int x, int y)
{
int z;
z = (x + y) +1;
return z;
}

return
There is no restriction of return statements in a
function
Whenever the control returns from the function
some value is definitely returned. It should be
accepted in the calling program.
e.g.
int sum;
sum = calsum(a,b,c);
here the value returned by calsum must be of type
int.
If you are not returning any value from a function
then it should be declared as void. (void is a
keyword).

THANK YOU.

You might also like