C Programming language

adplus-dvertising
Function in C: Types
Previous Home Next

What is function

A function is a self-contained block of statements that perform a coherent task of some kind.

Example

main( )
{
message( ) ;
printf ( "\nCry, and you stop the monotony!" ) ;
}
message( )
{
printf ( "\nSmile, and the world smiles with you..." ) ;
}

The output will be--
Smile, and the world smiles with you...
Cry, and you stop the monotony!

In the above program the main() itself is a function. through the main we are calling the function message().that mean the control passes to the function message(). when the message() function runs out of statement to execute, the control returns to the main().thus main() becomes the "calling function" where as the message() becomes the "called function".

Some important points in the the C for functions are given below:

  1. Any C program contains at least one function.
  2. If a program contains only one function, it must be main( ).
  3. If a C program contains more than one function, then one (and only one) of these functions must be main( ), because program execution always begins with main( ).
  4. There is no limit on the number of functions that might be present in a C program.
  5. Each function in a program is called in the sequence specified by the function calls in main( ).
  6. After each function has done its thing, control returns to main( ).When main( ) runs out of function calls, the program ends.

Thus, we can say that-

  1. A C program is a collection of one or more function.
  2. A function gets called when the function name is followed by a semicolon. For example:
    main( )
    {
    argentina( ) ;
    }
    
    A function is defined when function name is followed by a pair of braces in which one or more statements may be present. For example-
    argentina( )
    {
    statement 1 ;
    statement 2 ;
    statement 3 ;
    }
    
  3. Any function can be called from any other function. Even main( ) can be called from other functions. For example-
    main( )
    {
    message( ) ;
    }
    message( )
    {
    printf ( "\nCan't imagine life without C" ) ;
    main( ) ;
    }
    
    A function can be called any number of times. For example-
    main( )
    {
    message( ) ;
    message( ) ;
    }
    message( )
    {
    printf ( "\nJewel Thief!!" ) ;
    }
    

Types of functions

There are basically two types of function:

  1. library function (printf(), scanf())
  2. user defined function (brazil(), usa())

We can also describe the category of the function under it's type.

A function may belong to any one of the following categories:

  1. Functions with no arguments and no return values.
  2. Functions with arguments and no return values.
  3. Functions with arguments and return values.
  4. Functions that return multiple values.
  5. Functions with no arguments and return values.
#include<stdio.h>  

#include<conio.h>  

void add(int x,int y)  

{  

int result;  

result = x+y;  

printf("Sum of %d and %d is %d.\n\n",x,y,result);  

}  

void main()  

{  

clrscr();  

add(10,15);  

add(55,64);  

add(168,325);  

getch();  

}  

Library functions are nothing but commonly required functions grouped together and stored in what is called a Library.

Previous Home Next