User defined function in c programming

User Define Function in C programming.
User Define Function are those function which is develop by the users at the time of writing program. Main function of c programming is one of the user define function. User define function provide the reusability and its save time and space. 

User Define Function consist three elements.
  • Function definition
  • Function call
  • Function declaration

Function Definition

Function definition indicate that what will function do? The definition part must be exist at outside of main() function. It contain two parts :
  • Function header
  • Function body

Function Header
Function header consists three parts: the function type also known as return type, the function name and the formal parameter list.

Function Body
Function body contains the declaration and statements necessary for performing the required task.


A general syntax of function definition is:

function_type function_name(parameter_list)//function header
{
      //function body
       Statement 1;
       Statement 2;
        ................
       Statement n;
}

Function Call

A function can be called by simply using the function name with a list of actual parameters. It forward the control of main() to user define function.It terminate with semi-colon.

Syntax:
                 function_name( actual_parameters);


Function declaration

All function in a program must be declared before they are invoked. Function declaration is also called function prototype. Function declaration always terminated with semi-colon.

Syntax : 
                Function_type function_name( parameter-list);

Now lets we see the simple example of user define function. In which we simply create a function to print a line.

#include<stdio.h>

//Globle declaration of UDF.
void printline();

void main()
{
//local declaration of UDF.
void printstar();
clrscr();
printline();  //function Calling
printf("\nThis is an Example of User-Define Function\n");
printline();  //Function calling
printstar();  //function calling

getch();

}
//Function definition
void printline()
{
printf("-------------------------------");
}
//function definition
void printstar()
{
printf("\n**********************************\n");
}

0 comments:

Post a Comment