FoodForFutureGeeks

Monday 2 April 2012

Functions

function is an independent block of code, that is designed to carry out a specific task, it can accept certain values as input or it can have no input, it may return the results after completion or it may not.

General format of a  C function:

Returntype function name(formal arguments list)
ex: void add(int var1,int var2), void print(void);
sample program:
#include 
int add(int,int);
void main()
{
int sum=0,var1=0,var2=0;
printf("enter two variables to calculate the sum:\n");
scanf("%d%d",&var1,&var2);
//call to add function, main function is the calling function
sum=add(var1,var2);//var1 var2 are the actual parameters
printf("sum is:%d",sum);
}
int add(int value1,int value2)//value1,value2 are called as formal parameters
{
return value1+value2;
}//returns the sum

Return Type:

The return type indicates datatype of the value returned by the function on completion of its execution, if a function doesn't return any value then its return type will be void.
ex:
void add(int var1,int var2) ; this function does not return anything.
int add(int var1,int vbar2);  this function returns an integer value.
similarly a function can have return types as float,char etc.

Formal arguments/parameters:

formal arguments are the variables in called function,that accepts the value passed from the calling function.
ex: void add(int var1,int var2 ) here var1,var2 are the formal parameters.

Actual arguments/parameters:

actual arguments are the variables passed from the calling function like main in the sample program.

Function declaration/prototyping a function:

function declaration  is used to inform the compiler about the existence of a function body after the main, this is done by just defining the function signature ie the return type, function name, formal argument types/formal arguments.Since C is a top down programming language(read article on programming approaches c,c++), this should be the ideal approach ie all functions should be declared before main(), definition should be after the main().
ex:
//top down approach
#include 
//function declaration or prototype
int add(int,int);
void main()
{
int sum=0,var1=0,var2=0;
printf("enter two variables to calculate the sum:\n");
scanf("%d%d",&var1,&var2);
//call to add function, main function is the calling function
sum=add(var1,var2);//var1 var2 are the actual parameters
printf("sum is:%d",sum);
}
//function definition
int add(int value1,int value2)//value1,value2 are called as formal parameters
{
return value1+value2;
}//returns the sum

Calling a function:

A function can be called by using its name and by passing the arguments required in parenthesis.
ex:
    sum=add(10,20);
    sub(5,10); etc
the values passed should be of same datatype as mentioned in the declaration or definition.
if the function returns some value then we can accept the returned value by using a variable of same type.

Function definition:

a function definition has the actual code/instructions that enables the function to perform a specific task.ex: performing addition ,subtraction,calculate the simple interest etc.

Bottom up programming:

ex :read article programming approaches c,c++ in general programming
//bottom up approach
#include 
//function definition
int add(int value1,int value2)//value1,value2 are called as formal parameters
{
return value1+value2;
}//returns the sum
void main()
{
int sum=0,var1=0,var2=0;
printf("enter two variables to calculate the sum:\n");
scanf("%d%d",&var1,&var2);
//call to add function, main function is the calling function
sum=add(var1,var2);//var1 var2 are the actual parameters
printf("sum is:%d",sum);
}








2 comments:

  1. how to take the array result in function to be put to another function?

    ReplyDelete
    Replies
    1. sorry for late reply!!!Please go through the article on Pass By value & Pass By Reference in my blog, if u still have some doubts please feel free to ask.(This time ill respond faster).

      Delete