Function in C++

In this page we will discuss about C++ function and their Parameters type with following,


Function in C++

In simple way, The function is a set of statements in C++

let’s explore it,

It is not easy to manage many statements together in a single program, so we divided these into small pieces or modules. 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 in name in C++ are called function-Declaration.

Here is the syntex of Declaring a function in C++,

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 or Body of a 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 also call a function multiple times in the program as per our requirement.

Remember here,we can not call a function without its function definition if we do this a error will be occur in the program at run-time (not compile-time) known as Linker error.   

 

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 in C++

There are two types of function in c ++


Inbuilt function or library function in C++

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 in C++

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 declared in the calling function- header are called actual parameters. 

SYNTEX:

function-name(parameter-list);

Example:-

calculate(a,b);

a and b are called actual parameters. 

see the Diagram below,

function-in-cpph

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 a,b; // variable declaration

    cout<<"Enter two number: ";
    cin>>a>>b;

      calculate(a,b); // 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 task,

more about function,


previous-Memory Management and Types

next-String library function and theirs examples