User Defined function and it’s elements in C Programming Language

User defined Function:

A function is a self-contained program segment that carries out some specific, well-defined task. Every C program consists of one or more functions. a function will process information that is passed to it from the calling portion of the program, and return a single value. Information is passed to the function via special identifiers called arguments (also called parameters), and returned via the return statement.

Elements of the user defined function:

  1. Function definition
  2. Function call
  3. Function declaration

Function definition:

The function definition is an independent program module. The task of a function is defined in the function definition. A function definition shall include the following elements:

  1. Function name
  2. Function type
  3. list of parameters
  4. local variable declarations
  5. Function statements
  6. A return statement                                         Function call:

Function name, Function type and list of parameters, these three elements are in Function header and local variable declarations, Function statements and a return statement, these three elements are in Function body

In order to use the function every function need to invoke it at a proper place in the program. This is known as Function call. A function can be called by simply using the function name followed by a list of parameters.

Function declaration:

Like variables, all functions in a C program must be declared , before they are invoked. A function declaration  (also known as function prototype) consists of four parts:

  1. Function type(return type)
  2. Function name
  1. Parameter List
  2. Terminating semicolon

 

Example:                         

#include< stdio.h >

int add(int a, int b); //Function Prototype

int main()

{

int p,q,r; //local variable

scanf(“%d %d”,&p,&q);

//function call

r=add(p,q); // here p and q are actual parameters

printf(“%d”,r);

return 0;

}

//Function Definition

int add(int a, int b) // a and b are formal parameters

{

int c; // local variable

c=a+b;

return c;

}

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Lean Blog by Crimson Themes.