Вы находитесь на странице: 1из 19

A function is a self contained block of statements that

performs a task of some kind.


Every C program is a collection of these functions.

#include <stdio.h>
void main()
{
message();
printf(\n Ahmedabad);
}
void message()
{
printf(PRO);
}
O/P:

PRO
Ahmedabad

main() calls the function message()


Control passes to the message() function.
Activity of main() is temporally suspended.
After execution of message statements, control returns

to main and begin executing the code at the exact


point where it left.
main becomes the calling function
message becomes the called function

function name (argument declaration)


{
local variables declaration
executable statements

return(expression)
}

void main()
o/p: PRO
{
Ahmedabad
dispname();
www.xyz.com
dispcity();
+91-0000
dispweb();
dispcontact();
}
void dispname()
{printf(PRO);}
void dispcity()
{printf(Ahmedabad);}
void dispweb()
{printf(www.xyz.com);}
void dispcontact()
{printf(+91-0000);

No limit is there on number of functions.


Program execution always begins with main()
Any function can be called from any other function
A function can call itself. It is called Recursion.
A function can be called from any other function but a function
cant be defined in another function.
Following code will give an error.

main ( )
{

function ( )
{
task
}
}

In C there are two types of functions.

1) Library Functions
2) User Defined Functions
Library Functions : Commonly required functions
grouped together and stored in what is called a
Library.
This Library is present on the disk and is written for us
by peoples who write compilers for us.

Avoids rewriting the same code again & again.


Easier to write programs & keep track of what they are

doing.
Operation of the program is divided into different
activity & each activity is placed in different functions.
Thus separating code into modular functions also
makes the program easier to design and understand.
Dont try to pack the whole logic in one function.

The mechanism used to convey information to the

function is the argument.


The arguments are also called parameters.

void main()
{
int a, b, c, avg;
printf(Enter 3 numbers.).
scanf(%d %d %d,&a,&b,&c);
avg= findavg(a, b, c);
printf(Average is %d. , avg);
}
int findavg(int x, int y, int z)
{
int ans;
ans = (x + y + z)/3;
//int. average
return(ans);
}

a, b & c are called Actual Arguments


x, y & z are called Formal Arguments
Any number of arguments can be passed to a function.
Type, order and number of the actual & formal

arguments should be same.

Method 1

function ( a, b, c )
int a , b , c ;
Here declaration is done in separate statement.
This method is known as K & R method.
Method 2

function ( int a, int b, int c )

Immediately transfers the control back to the calling

function
Returns the value present in ( ) example : return ( a )
return ;
will return a garbage value to the calling function
if we want that the called function should not return a
value at that time we must use void type for the
function.
Example:
void display ( )
{
function body
}

A function can return only one value at a time.


Any C function by default returns an integer value.
If we want to return a value other than integer, it

should be mentioned in the calling function as well as in the called


function.
main ( )
{
float func_a ( argument type);

}
float func_a( arguments );
{
function body
}

Call by Value :
on calling a function we are passing values of variables
to it.
Call by Reference :
Instead of passing the value, we pass the address of the
variables

int square (int x)


{
return x*x;
}
int main ( )
{
int num = 10, ans;
ans = square(num);
printf(Answer is : %d , ans); // answer is 100
printf(Value of a is : %d , num); // num will be 10
}

The parameter is actually a copy of the argument.


The function will operate only on the copy and not on

the original argument.


This method is known as PASS BY VALUE.
You can see that the value of num is unchanged.
The function square works only on the parameter
(i.e. on x ).
It does not work on the original variable that was
passed (i.e. it doesn't work on num).

THANK YOU

Вам также может понравиться