Functions in C++ programming



Definition of C++ function

A function is a collection of statements that performs a specific task. So far you have used functions in two ways:

  • you have created a function called main in every program you’ve written.
  • you have called library functions such as pow and sqrt.

Functions are commonly used to break a problem down into small manageable pieces, or modules.

Instead of writing one long function that contains all the statements necessary to solve a problem, several smaller functions can be written, with each one solving a specific part of the problem.

Defining and calling functions

A function call is a statement that causes a function to execute. A function definition contains the statements that make up the function. When creating a function, you must write its definition. All function definitions have the following parts:

  • Name. Every function must have a name. In general, the same rules that apply to variable names also apply to function names.
  • Parameter list. The parameter list is the list of variables that hold the values being passed to the function. If no values are being passed to the function, its parameter list is empty.
  • Body. The body of a function is the set of statements that carry out the task the function is performing. These statements are enclosed in a set of braces.
  • Return type. A function can send a value back to the program module that called it. The return type is the data type of the value being sent back.

A function is executed when it is called. Function main is called automatically when a program starts, but all other functions must be executed by function call statements.


    #include <iostream>
    using namespace std;
    void displayMessage() // create displayMessage function
    {
    cout << "Hello from the function displayMessage.\n";
    }
    int main()
     {
    cout << "Hello from main.\n";
     displayMessage(); // Call displayMessage function
     return 0;
     }

    Output:
    Hello from main.
    Hello from the function displayMessage.

Each call statement causes the program to branch to a function and then back to main when the function is finished.
Any argument listed inside the parentheses of a function call is copied into the function’s parameter variable.

In essence, parameter variables are initialized to the value of the corresponding arguments passed to them when the function is called.

The program demonstrates a function with a parameter.


    #include <iostream>
    using namespace std;
    // Function prototype
    void displayValue(int num);
    int main()
     {
    cout << "I am passing several values to displayValue.\n";
    displayValue(5); // Call displayValue with argument 5
    displayValue(10); // Call displayValue with argument 10
    displayValue(2); // Call displayValue with argument 2
    displayValue(16); // Call displayValue with argument 16
    cout << "Now I am back in main.\n";
    return 0;
    }
    void displayValue(int num)
     {
    cout << "The value is " << num << endl;
     }

    Output:
    I am passing several values to displayValue.
    The value is 5
    The value is 10
    The value is 2
    The value is 16
    Now I am back in main.

The displayValue function is called four times, and each time num takes on a different value. Any expression whose value could normally be assigned to num may be used as an argument.

When a function is called, it is best if each argument passed to it has the same data type as the parameter receiving it.


PROMOTIONS