function and their types | Parameters

In this page we will discuss following topics


Function

In simple way, The function is a set of statements.

It is not easy to manage many statements together in one program, so these small pieces or modules are divided into them. These set of statements (which contain the code to perform a particular task) are called from the other part of the program. These modules are called function.


function declaration/prototype

declaring a function name are called function-declaration.

SYNTEX:

return-type function-name(parameter-list);

Here the function-name is an identifier i.e. any appropriate name to the function while parameter-list are data types.


function definition/body of function

This is the main body of the function in which the code is written to perform a task, we call the definition of any function to the other part of the program. We can call a function more than once in the program as per our requirement. Note can not call the function call without function definition.

 

use of function

  • The program can be easily read.
  • Easy debugging of program.
  • There is no need to write an extra statement to perform the same task, ie these can be reuse to perform the same task. which saves time.
  • Because using the function does not have to be written repeatedly for the same task, so the size of the program is reduced which increases the speed of the program.


Type of function

There are two types of function in c ++

 

inbuilt function/library function

Such functions are already defined in the library of C ++, we can use them by including them in the header file program! The use of inbuilt function in a program makes the program easy, meaning that we do not need to code separately for any task. For example-

clrscr () is used to clear the monitor screen.
For which, separately, we do not have to write code to clear the screen. We use this function only by including conio.h (in which it is defined) in the program.other function- setw (), getch (), getche (), swap () etc.

 

user-defined function

These are the functions that programmers create themselves to perform a task. Their definition consists of a code or set of statement to perform a task. In C ++ we can declare the function in three ways which are as follows-

 

function with no argument and no return value

when we declare the function without parameter-list, these are also called void type functions, because in the function declaration, void declare inside the bracket in the function header. Whoever does not return value.

SYNTEX:

void function-name(void);

Here is the program , where void sum(void); is function type

#include<iostream.h>
#include<conio.h>

void sum(void); // function declaration

 void main()
  {
    clrscr();
    sum(); //function calling
    getch();
  }

  void sum() // function definition
   {
      int num1,num2,total; // variable declaration

      cout<<"Enter two number: ";
      cin>>num1>>num2;

      total=num1+num2;
      cout<<"Total of these Number: "<<total;
   }
OUTPUT:-
Enter two number: 5 6
Total of these number: 11

 

function with argument but no return value

In this we also define some parameters (data type) inside the bracket in their header while making the function declaration, because their return type is void type data type so they do not return any value.

SYNTEX:-

void function-name(data-type);

Here is the program,

#include<iostream.h>
#include<conio.h>

void sum(int,int);          // function declaration

 void main()
  {
    clrscr();
    int num1,num2; // variable declaration

    cout<<"Enter two number: ";
    cin>>num1>>num2;

    sum(num1,num2); // function calling

    getch();
  }

  void sum(int x,int y) // function definition
    {
       int total;
       total=x+y;

       cout<<"Total of these Number: "<<total;
    }
OUTPUT:-
Enter two number: 7 8
Total of these number: 15

 

function with argument with return value

In this type of function declaration, we first declare return data type (except for void) , second function name and last declare some parameters in parenthesis bracket. These values return. Which can be of type int, char or float. These are not functions of type void!

SYNTEX:

data-type function-name(data-type);

Here is the program,

#include<iostream.h>
#include<conio.h>

 int sum(int,int); // protoype declaration

 void main()
  {
     clrscr();
     int num1,num2,total; // variable declaration
     cout<<"Enter two number: ";
     cin>>num1>>num2;

     total=sum(num1,num2); // function calling

     cout<<"Total of these Number: "<<total;
     getch();
  }

  int sum(int x,int y) // function definition
   {
      return x+y;
    }
OUTPUT:-
Enter two number: 6 8
Total of these number: 14

 

actual parameter

The parameters defined in the calling function are called actual parameters.

SYNTEX:

function-name(parameter-list);

Example:-

calculate (num1,num2);

num1 and num2 are called actual parameters.

 


formal parameter

Parameters defined in the function definition header are called formal parameter.

SYNTEX:-

function-name(parameter-list)
  {
     Body of function;
  }

Example:-

calculate (int x , int y)
  { 
     body of function;
  }

Here x and y are formal parameters.

Let’s try with an example

#include<iostream.h>
#include<conio.h>

 void calculate(int,int); // function declaration

 void main()
  {
    clrscr();
    int num1,num2; // variable declaration

    cout<<"Enter two number: ";
    cin>>num1>>num2;

      calculate(num1,num2); // function calling (actual parameter)

    getch();
  }
  
  void calculate (int x,int y) // function definition (formal parameter)
   {
      cout<<"\nmultiplication: "<<x*y;
      cout<<"\naddition : "<<x+y;
      cout<<"\nsubstraction : "<<x-y;
   }
OUTPUT:-
Enter two number: 4 5
multiplication: 20

addition : 9
substraction : -1

 

things to know

Function is a method that provides the facility to reuse the code to perform the same task, whereas code from control statement is written for a task. Therefore it is important to understand the control statement closely. Data Structure is based on control statement, array and pointer.

Related Exercise

more about function,


previous-Memory Management and Types

next-String library function and theirs examples