Thursday, September 24, 2009

FUNCTIONS

Definition:


A function is self contained blocks which perform a single task.

Any one of the task (number statements) can be repeatedly used. Then they are keeping in one function. i.e., without rewriting the same code repeatedly.

In function mainly contained three steps:

1) Function declaration

2) Function calling

3) Function definition

Local and Global variables of the function:

Local variables:

These variables are local to the block.

These variables are existing to the function itself.

They are unknown to the main() program.

Each time recreated when the function is call or executed.

Global variables:

These variables can be accessed in any of the function in the program.

Each time they do not create when the program call or executed.

If a variable of same name is declared within a function and outside of the function, the function will use local variable and ignore global variable.

Function declaration:

It is used to identify function return type, number of arguments, type of argument and function name.

It is declared above the main() .

Syntax:

Return type function name (argument1,argument2,..);

Example:

int add(int i,int j);

Function calling:

If we want to use functions using with the function call.

One function can call another function also.

Syntax:

With return value:

Return type variable=Function name (variable/value,...);

Without return value:

Function name (variable/value,.....);

Function definition:

In function definition to write the actual task (code of the function).

Syntax:

Function name (argument1,....)

{

Statement1;

....

Statement n;

return(value/variable);

}

If there is no return type then the statement is return 0;

Example 1:

Without return type and without arguments:

add(); //Function declaration

main()

{

add(); //Function calling

}

add() //Function definition

{

int i,j,res; //local variables to the function

printf(“\n Enter i and j values:”);

scanf(“%d %d”,&i,&j);

res=i+j;

printf(“\nResult :%d”,res);

}

Output:

Enter i and j values:2 3

Result:5

Example 2:

With return type and without arguments:

int add(); //Function declaration. int is return type

main()

{

int result;

result=add(); //Function calling. Here return value will accept result variable

printf(“Result i s:%d”,result);

}

add() //Function definition

{

int i,j,res; //local variables to the function

printf(“\n Enter i and j values:”);

scanf(“%d %d”,&i,&j);

res=i+j;

printf(“\nResult :%d”,res);

}

Enter i and j values:2 3

Result:5

Example3:

With return type and with arguments:

int add(int ,int); //Function declaration. int is return type

main()

{

int result,i,j;

printf(“\nEnter i and j values:”);

scanf(“%d %d”,&i,&j);

result=add(i,j); //Function calling. Here return value will accept result variable

printf(“Result i s:%d”,result);

}

add(int i,int j) //Function definition

{

int res;

res=i+j;

return res;

}

Output:

Enter i and j values: 2 3

Result is:5

Example 4:

With return type and with arguments using with global variables:

int add(int ,int); //Function declaration. int is return type

int subt(int ,int);

int i,j,result;

main()

{

printf(“\nEnter i and j values:”);

scanf(“%d %d”,&i,&j);

result=add(); //Function calling. Here return value will accept result variable

printf(“\n Add() Result i s:%d”,result);

result=subt();

printf(“\n sub() Result:%d”,result);

}

add(int i,int j) //Function definition

{

result=i+j;

return result;

}

subt(int i,int j) //Function definition

{

if(i>j)

result=i-j;

else

result=j-i;

return result;

}

Output:

Enter i and j values:2 4

Add() Result is:6

Subt() Result is:2

1 comment: