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:
Syntax:
return data_type function_name(arguments list);

Function Definition: 
Syntax:
return data_type function_name(argument list)
{
Body;
}
Function Calling:
Syntax:
Function_name(param_list);
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;
    ----------------
    ----------------
    return(expression);
}

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.

Eg:
01#include < stdio.h>
02#include < conio.h>
03void hai();      /* Function Declaration */
04void main()
05{
06     
07   clrscr();
08   hai();      /* Function Calling */
09   getch();
10}
11 
12void hai()      /* Function Definition*/
13{
14   printf("\n WELCOME TO THE WORLD OF C LANGUAGE");
15}
16                                

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.

Output:
WELCOME TO THE WORLD OF C LANGUAGE 
Example program that shows the difference between calling and called functions.
01#include< stdio.h>
02#include< conio.h>
03void message();
04void main()
05{
06    message();
07    printf("calling function is main");
08}
09void message()
10{
11    printf("This function is called function which is called by main");
12}
13                                    

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

No comments:

Post a Comment