User-defined Functions

User can define functions to do a task relevant to their programs. Such functions are called user-defined functions. The main( ) function in which we write all our program is a user-defined function. Every pro0gram execution starts at the main( ) function.
Any function (library or user-defined) has 3 things

  1. Function declaration
  2. Function calling
  3. Function defintion

In case of library functions, the function declaration is in header files. The function is library files and the calling is in the program. But in case of user-defined functions all the 3 things are in your source program. Function declaration:
return data_type function_name(arguments list);

Function Definition:
return data_type function_name(argument list)
Function Calling:
Formal Arguments:
The arguments which are given at the time of function declaration or function definition are called formal arguments.

Actual Arguments:
The arguments which are given at the time of function calling are called actual arguments.

General Form of a Function

return data type  function-name(arglist)
argument declaration
    Local variable declaration;
    Executable statements;

All parts are not essential. Some may be absent.
For eg: The argument list and it’s associated argument declaration part are optional.
We may recall that the main functions discussed so far have not included any arguments.

return: This statement is for returning a value to the calling function. This is an optional statement. It’s absence indicates that no value is being return to the calling function.

Function name: A function must follow the same rules of formation as other variable names in C. Additional care must be taken to avoid duplicating library functions names or operating system commands.

Argument list : It contains valid variable name separated by commas. The list must be surrounded by parenthesis. Note that no semi colon follows the closing parenthesis. The argument variables receive values from the calling function. Thus providing a means of data communication from calling function to the called function.

If the arguments are present before beginning with the statements in the functions it is necessary to declare the type of arguments through the type declaration statements.

Note: There are two methods of declaring the parameters. The older method (known as "classic" method) declares function arguments separately after the definition of function name (known as function header).

The newer method (known as "modern" or ANSI method) combines the function definition and argument declaration in one line. Luckily the modern compilers support both the methods. That is, we can write a function using either of them and execute it successfully.


#include < stdio.h>
#include < conio.h>
void hai();      /* Function Declaration */
void main()
   hai();      /* Function Calling */

void hai()      /* Function Definition*/

Execution of the program:
The program execution normally starts at the main function. After clearing the screen, the control of the program is taken to the hai( ) function. The executable statements in it i.e, the printf statement is executed. Now the control again is taken to the getch( ) function where it waits for a character to be given by the user.

Example program that shows the difference between calling and called functions.

#include< stdio.h>
#include< conio.h>
void message();
void main()
	printf("calling function is main");
void message()
	printf("This function is called function which is called by main");

OUTPUT: This function is called function which is called by main.
calling function is main.

Note: In the above program main () --------calling function and message () ----called function