Tuesday, 16 October 2012

User Define Function

What is User Define Function?
A function that is declare, calling and define by the user is called user define function.
Every user define function has three parts as:

  1. Prototype or Declaration
  2. Calling
  3. Definition
1. Prototype or Declaration

Prototype/declaration set the outline of function like as:
    function is something returning or not?
    what is function name?
    what would be the nature of parameter?
    how much parameters used in function?
This is generally written at top of program like as after or before the main() function.

syntax:
return_data_type function_name(data_type parameter, ......);

Description of syntax:

In above syntax "return_data_type" may be two types:
if return_data_type is void: This mean that our function nothing return.
if return_data_type is other like as int, float, double etc. then it would be return as int, float, double etc. respectively.
for example:
int factorial(int num); //it would return a integer value.
void display(int num); //it would nothing return.

In above syntax "function_name" means that the name of function which we want to make.
for example:
int myfunction(int i);

In above syntax "data_type parameter" means that first write down the data type( int, float, double, long, char ) and after write down the name of related parameter/argument/variable. It is may be one, two and so on as the requirement of function.
for example:
int sum(int x, int y);
int myfunction(int name);

In Prototype or Declaration of function, we can write only the name of data type, not require the name of parameter/argument( it is optional ).
for example:
int sum(int , int );  //it would return a integer value.
int myfunction(int );  // this is valid and correct
int myfunction(char ); // this is valid and correct


2. Calling

The calling part of function work as:
    It is call the function.
    It forward the control of main() to user define function and vice versa.
The calling is generally written as middle of program after grab all related argument.

syntax:
//if return type is void:
function_name(actual argument/parameter);

//if return type is not void:
return_parameter=function_name(actual argument/parameter);


for example:
display(num);   //void calling function- display
result=sum(x, y)  //return calling function-sum
f=factorial(num)  //return calling function- factorial
ex=myfunction(number)  //return calling function - myfunction

3.  Definition

The "definition" part is the heart of function.
    What will function do? It define the all function body.
The definition part must be exist at outside of main() function. i.e. it is written after making all the program.

syntax:

//if return type is void:
function_name(data_type argument/parameter)
{
   statement 1;
   statement 1;
   statement 1;
          .
          .
          .
   statement n;
}

//if return type is not void:
return_data_type function_name(data_type parameter)
{
   statement 1;
   statement 1;
   statement 1;
          .
          .
          .
   statement n;
   return ( any_return_value);
}

example:
int sum(int x, int y)
{
  int res;
  res = x+y;
  return(res);
}

A complete example of user define function:

/*demonstration of user define function-sum*/
#include<stdio.h>
int sum(intint);  //Prototype or Declaration
int main()
{
  int x,y,result;
  printf("Enter value of x and y: ");
  scanf("%d %d", &x, &y);
  result=sum(x, y);  //calling of function-sum
  printf("Sum of %d + %d = %d",x,y,result);
  return 0;
}

//Definition of function sum
int sum(int a, int b)
{
  int res;
  res = a + b;
  return ( res );
}

The output of above program would be:

Output of user define function sum C program
Figure: Screen shot for user define function
sum C program

No comments:

Post a Comment