Functions

0 laureney2075516 November 13, 2020

A function is defined as a self-contained block of statements that performs a particular task or job. This is a logical unit composed of a number of statements grouped into a single unit. It is often defined as a section of a program performing a specific job. Each program has one or more functions.

We have many advantages in using functions.

  • Manageability
  • Code Reusability
  • Non-redundant (non-repeated) programming (i.e. avoids redundant programming)
  • Logical Clarity
  • Easy to divide the work into many different programmers

The syntax of the function is:

return_type function_name (parameter_list)
{
   //Statement(s)
}

Let’s take an example to make clear concepts on function.

#include <iostream> 
using namespace std;

int sum(int a, int b){
    return a + b;
}

int main() 
{

    //calling `sum` function 
    cout << "Sum of 5 + 6 = " << sum(5, 6) << endl;
    cout << "Sum of 10 + 5 = " << sum(10, 5) << endl;

    return 0; 
}

The output of the above program is:

Sum of 5 + 6 = 11
Sum of 10 + 5 = 15

In this example, a function called sum() is defined once and it is called two times to calculate the sum of two numbers. The detail of the defining function will be discussed later in this chapter.

Function Declaration or prototype

The function declaration or prototype is a model or blueprint of the function. If the function is used before they are defined, then a function declaration or prototype is necessary which provides the following information to the compiler.

  • The name of the function
  • The type of value returned by the function
  • The number and the type of arguments that must be supplied while calling the function.

When a user-defined function is defined before the use, there is not need for function declaration.

The syntax of function declaration is:

return_type function_name (type1, type2, type3, ....)

Here return_type specifies the data type of the value returned by the function. 

The function declaration and declator or header in function definition must use the same function name, number of arguments, argument types and return types. 

We will write above program using function declaration.

#include <iostream> 
using namespace std;

//function declaration or prototype
int sum(int a, int b);

int main() 
{

    //calling `sum` function 
    cout << "Sum of 5 + 6 = " << sum(5, 6) << endl;
    cout << "Sum of 10 + 5 = " << sum(10, 5) << endl;

    return 0; 
}

int sum(int a, int b){
    return a + b;
}

The output of above program remains same. It is important to note that the function prototype has semicolumn at the end and to specify the name of formal arguments/variables is not mandatory but the type is mandatory.

Types of Fuction

We have two types of functions in C++ programming language.

Types of Function
Types of Function

1. Built-in Function

There are the functions which are already written, compiled  and placed in C++ library and they are not reuired to be written by a programmer. The function’s anme, its return type, their argumen number and types have been already defined. We can use these functions as required. For example coutcinetc are the example of library functions.

Let’s look at one example to make clear concepts on built-in function.

#include <iostream>
#include <cmath> 
using namespace std;


int main(){

    /**
     * Calling built-in function
     * pow(x, y)
     * We are directly calling this function
     * */
    cout << pow(2, 3);

    return 0;
}

The output of the above program is:

8

In the above program, We have used pow() function is already written in cmath library. This function helps us to find the power of any number.

User-defined Function

There are functions which are defined by user at the time of writing a program. the user has choice to choose its name, return type, arguments and their types. In the above example, the function sum() is  user-defined function. 

A complex program can be divided into a number of user-defined functions.

Let’s look an example to make clear concepts on user-defined functions.

#include <iostream>
using namespace std;

int sum(int x, int y);

int main(){

    int x, y;

    cout << "Enter First Number : ";
    cin >> x;

    cout << "Enter Second Number : ";
    cin >> y;

    /**
     * Here we are calling user-defined functions
     * named as sum
     * */
    cout << "Sum = " << sum(x, y) << endl;

    return 0;
}

int sum(int x, int y){
    return x + y;
}

The output of the above program is:

Enter First Number : 12
Enter Second Number : 23
Sum = 35