C Programming Tutorial
Computer Tutorial

Function



What is a Function

A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions.

The general form of a function is :
return-type function-name ( parameter list )
{
        body of the function
}

The return-type specifies the type of data that the function returns. A function may return any type of data except an array.The parameter list is a comma-separated list of variable names and their associated types. The parameters receive the values of the arguments when the function is called. A function can be without parameters, in which case the parameter list is empty. An empty parameter list can be explicitly specified as such by placing the keyword void inside the parentheses.

A Program to find the square of number usning Function

\* C Program to find the square of number usning Function *\

# include < stdio.h >
int  sqr (  int x) ;
int   main( )
{

int   t = 10  ;
printf(" %d %d ",  sqr(t), t) ;
return ( 0 ) ;

}
int  sqr (  int x
{

x = x * x   ;
printf(" %c ",  name[i]) ;
return ( x ) ;

}

Remember the following point about a function

1. C program is a collection of one or more functions.
2. A function gets called when the function name is followed by a semicolon.
3. A function is defined when function name is followed by a pair of braces in which one or more statements may be present.
4. Any function can be called from any other function. Even main( ) can be called from other functions.
5. A function can be called any number of times.
6. The order in which the functions are defined in a program and the order in which they get called need not necessarily be same.
7. A function can call itself. Such a process is called ‘recursion’.
8. A function can be called from other function, but a function cannot be defined in another function.