MY mENU


Wednesday 15 February 2012

Functions in C


Functions:

A number of statements grouped into a single logical unit are called a function. The use of function makes programming easier since repeated statements can be grouped into functions. Splitting the program into separate function make the program more readable and maintainable.

It is necessary to have a single function ‘main’ in every C program, along with other functions used/defined by the programmer.

A function definition has two principal components: the function header and body of the function. The function header is the data type of return value followed by function name and (optionally) a set of arguments separated by commas and enclosed in parenthesis. Associated type to which function accepts precedes each argument. In general terms function header statement can be written as

return_type function_name (type1 arg1,type2 arg2,..,typen argn)

where return_type represents the data type of the item that is returned by the function, function_name represents the name of the function, and type1,type2,…,typen represents the data type of the arguments arg1,arg2,..,argn.Here is a very simple function, which accepts one argument, multiplies it by 2, and hands that value back:

int add(int p,int q)
{
return p+q; //Body of the function
}

Here p and q are arguments. The arguments are called formal arguments or formal parameters, because they
 represent the name of the data item that is transferred into the function from the calling portion of the program. The corresponding arguments in the function call are called actual arguments or actual parameters, since they define the data items that are actually transferred.

A function can be invoked whenever it is needed. It can be accessed by specifying its name followed by a list of arguments enclosed in parenthesis and separated by commas.
e.g., add(5,10);
The following condition must be satisfied for function call.

1.The number of arguments in the function calls and function declaration must be same.

2.The prototype of each of the argument in the function call should be same as the corresponding parameter in the function declaration statement.


 A function may or may not return a value. A ‘return’ statement returns some value to the calling function and it may be assigned to the variable in the left side of the calling function. The return value can be a constant, variable, a user defined data structure, a general expression, a pointer to function, or a function call. The return statement also causes the program logic to return to the point from which the function was accessed. In general term the return statement is written as


return expression;


                  If a function does not return a value, the return type in the function definition and declaration is specified as void. The declaration for a prototype for a function that receive any arguments from the calling function and does not return any value will have the format
                                                void function_name (void);

No comments:

Post a Comment