Topic 8: Functions (Pass by value)

A function is a block of code grouped with a name and performs a task. Every C++ program contains at least one function i.e. the main function. Some functions are built-in functions, defined in libraries e.g., pow, sqrt, time etc. Others, called program-defined functions, are written by programmers.

Functions allow for blocks of code to be used many times in a program without having to duplicate code. They also allow large, complex programs to be broken down into small, manageable sub-tasks. Each sub-task is solved by a function, allowing different people to write different functions.

Many functions can then be combined into a single program. Typically, the main function calls other functions, but any function can call any other function.

There are two types of functions:

  • Built-in Functions: Pre-defined functions provided by libraries like pow and sqrt.
  • User-Defined Functions: Functions defined by programmers to perform specific tasks.

Built-in Functions

The pow function is used to raise a number to a power (exponentiation). Its syntax is pow(x, y), where x is the base and y is the exponent. At least one of the two arguments must be a double.

The sqrt function returns a number’s square root as a double. Its syntax is sqrt(x), where x is a double or float.

These functions are defined in the cmath library, so your program must include the directive #include .

Examples of built-in functions:

Example 1: Calculating 2^3
  • cout << pow(2.0, 3);
Example 2: Calculating square root of 100
  • cout << sqrt(100.0);

User-Defined Functions

A program-defined value-returning function definition consists of a header and a body. The header (first line) includes the return data type, function name, and an optional parameter list.

Syntax:

Function Syntax
Figure 8.1: Function Syntax

Function names follow the same rules as variables. It’s advisable to use meaningful names that describe the function’s purpose, preferably verbs.

The memory locations in the parameter list are called formal parameters, each storing an item of information passed to the function when it’s called.

The function body contains instructions for performing the function’s assigned task, enclosed in braces ({}). The last statement is typically the return statement, which returns one value (matching the return data type in the function header).

Functions can be called from the main function or other functions.

Calling a Function

A function must be called (invoked) to perform its task. The main function is automatically called when the program runs, while other functions must be called by a statement.

Syntax:
functionName(argumentList);

  • argumentList contains actual arguments (if any).
  • An actual argument can be a variable, named constant, literal constant, or keyword.

C++ allows passing either a variable’s value or its address to a function. Passing a variable’s value is passing by value, while passing its address is passing by reference. The choice depends on whether the receiving function needs to modify the variable’s contents.

Passing by value will not permit the function to change the contents of the variable, but passing by reference will. Passing a variable by value means that only a copy of the variable’s contents is passed, not the address of the variable. This means that the receiving function cannot change the contents of the variable

The number, data type, and ordering of actual arguments must match the formal parameters in the function header.

Function Prototypes

When a function definition appears below the main function, a function prototype must be written above the main. It specifies the function’s name, return value data type, and each formal parameter’s data type (if any). Formal parameter names in the prototype are optional.

Syntax:
returnDataType functionName(parameterList);

Example 1: Function Prototype with Named Parameters
  • double calculatePay(double hoursWorked, double rate);
Example 2: Function Prototype with Unnamed Parameters
  • double calculatePay(double, double);

Void Functions

Void functions perform tasks like value-returning functions but do not return a value. The function header starts with the void keyword instead of a return data type. The function body does not include a return statement. Call a void function with its name and actual arguments (if any) in a statement.

Syntax:
  1. void functionName([parameterList])
  2. {
  3. statement;
  4. }
Example: To call the following function simply say displayCompanyInfo();
    1. void displayCompanyInfo()
    2. {
    3. cout << “XYZ Company Ltd” << endl;
    4. cout << “Fiji Islands” << endl;
    5. }