You are on page 1of 1

Exam

Review 2022
5/12/22 7:36 PM

Functions:
- A function is a collection of statements that performs a specific task.
- A function call is a statement that causes a function to execute.
- A function definition contains statements that make up the function.

Parts of a Function:

Name
- All rules that apply to variable names apply to function names.
Parameter
- The list of variables that hold values being passed to the function. Enclosed in parentheses. If none is
being passed to the function keep parameter list empty.
Body
- Set of statements that carries the task the function is performing. These are enclosed in braces.
Return Type
- The data type of the value being sent back to the program module.

Figure 6.2 Below shows the definition of a simple function with the parts labeled.

The first line is called the function header. This is where we'll first look at the return type. Then comes the function
name. The header ends with a set of parentheses whether or not there's a parameter, if there are any parameters
they are to be listed inside.

Void Functions:
- These functions simply perform one or more statements and then return.

The function perfectly describes the function and what it's going to do. Additionally, no parameters are
needed as it does not need to receive any information to do its job. The return type is void as it does not
need to return a value when it is finished executing and returns to the program. Hence, no return statement
is needed. The program ends when the brace is encountered, and the program automatically returns.

Calling a Function:
- A function is executed when it is called. And in order for a function to be called it needs function call
statements.
- When a function is called it branches off to the function and executes the statements in its body.

All C++ programs start by executing the main. Other functions are only executed when they are called. Pay
attention to how the function is called. It is similar to the function name, but it removes the return type and
adds a semicolon to end off.
- Note: on line 6 is where the Function Prototype is shown it lets the compiler know that the function will
appear later in the program. It looks similar to the function header, but ends in a semicolon.

- A function can also be called in a loop or if statement.


See program 6-2 below:

Tells the amount of times the loop will run.

- There can be more than one function calls in a program.


See program 6-3 below:

Pay attention to how the functions are


formatted

Function Prototypes:

- Eliminates the need to place a function definition before all calls to the function.
The compiler must know a few things about the function before it encounters the call; for
example, the number of parameters, the data type for each function, and the return type.
If there is no prototype the function needs to be placed before the main function.

Sending Data into a Function:

- When a function is called a program may send data into the function.
- Values that are sent into a function are called "Arguments"; for example, in the statement
result = pow (2.0, 3.0) ;
The function "pow" is being called with two arguments, 2.0 and 3.0 being passed to it.
The pow function uses the information passed to it to compute the value of the first
argument raised to the power of the second argument . In this case, it will compute the
value of 2.0 to the third power and return the value 8.0 to be stored in result.
- A parameter is a unique variable that holds a value that is passed as an argument in a
function; for example, this definition of a function that has a parameter.
Void displayValue(int num)
{
cout << "The value is" << num << endl;
}

This function can now accept an integer to be passed to it as an argument, as it was defined in
the parameter list.
For a better example take a look at Program 6-6 below:

- Different arguments can be passed to the function whenever it is called in the program.
Take a look at program 6-7: See how different arguments are passed each time the function is
called. Each argument must have the same data type as the parameter it is passed to

- More than one arguments can often be passed to a function.


View program 6-8 below. It shows how three arguments are passed to a function. Pay attention
to the how the parameters are defined, as well as the function prototype.

- As you can see in the function header, each parameter has its data type in front of it;
however, unlike regular variable definitions they cannot be combined behind one single
data type eg. (int num1, num2, num3) despite their similar data type.
- Another point to look at is how although the function header and prototype must list the
data type of each parameter, the call function must not list any data types.
- If the call were to have a data type it would cause an error. Figure 6-7 below shows how
the function would run without the data types, and that when a function with multiple
parameters is called, the arguments are passed to the parameters in order.

You might also like