Chapter 04 Procedural Abstraction - Function
3. Function
3.1 Function Definition
<type-of-return-value> <function-name> (<parameter-list>) <function-body>
<type-of-return-value>
describes the data type the function will return. If it is void, the function won’t return anything.<function-name>
is the name of the function, which is a identifier.<parameter-list>
contains the names of parameters, in the form of<data-type> <parameter-name>
separated by commas.<function-body>
is a compound statement which usually contains a return statement which means end the function and return the value of the expression comes after it.
Notice that we are not allowed to jump out of the function body using
goto
statement.Remember that each C++ program must have a function called
main
, which describes the whole procedure of the program and call some other functions help solve the problem.
3.2 Function Call
<function-name> (<argument-list>)
Argument-list as a list of arguments separated by commas and correspond to the definition of the function.
The execution rule for function call is that:
- Evaluate the arguments and get the values of them while which is evaluated is not regulated.
- Pass the value of each argument to the corresponding parameter.
- Execute the function body.
- Return the value when encountering the return statement if there is one.
Notice that a function call is also an expression in which the function name works as an operator and arguments works as operands.
As to a function call that has no return value, we must match it with a
;
, so that we get a statelemt.
3.3 Function Delcaration
<type-of-return-value> <function-name> (<parameter-list>);
A function must be defined or declared before we call it since C++ program is top-down.
We use function prototype to declare a function and we can just list types of parameters in the parameter-list without declaring their name.
3.4 Call-by-Value
Now we’ll use an example to demonstrate how values are passed to a function and returned.
#include <iostream>
using namespace std;
double power(double x, int k){
if (x == 0) return 0;
double product = 1.0;
if (n >= 0)
while (n > 0){
product *= x;
n--;
}
else
while (n < 0){
product /= x;
n++;
}
return product;
}
int main(){
double a = 3.0, c;
int b = 4;
c = power(a, b);
cout << a << "," << b << "," << c << endl;
return 0;
}
- When executing
main
, the memory will allocate space to variablea
,b
andc
; - When calling function
power
, the memory will also allocate space tox
,n
,product
and usea
,b
,1.0
to initialize them; - When function
power
ends its execution,x = 3.0; n = 0; product = 81.0
; - The return value
81.0
will be passed toc
and the space ofx
,n
,product
will be released.
From the process above we can conclude that the arguments
a
andb
won’t be affected during the function call, which indicates that call-by-value will do nothing to the outside scope and have no side effects other than output which actually is not a side effect in C++ programs.
3.5 Global Variable and Local Variable
In addition to abstraction, function also features the role of encapsulation, which means everything in the function body is invisible to outside the function body, which is also called information hiding.
- Local Variable refers to variables defined in a compound statement, which means this variable is only effective in this compound statement.
Function body is also a compound statement, so the parameters are only effective in the function body.
Local variable shows the function’s feature of encapsulation and information concealment, which indicates that variables in different scopes could have the same name.
-
Global Variable refers to the variables defined outside any function including the
main
function, which means they can be accessed and used by any function in this program.The global variable must be defined before we use it or we should give it a declaration.
extern <data-type> <variable-name>;
Variable definition is also a kind of declaration called definitional declaration in comparison with non-definitional declaration.
The difference between variable definition and variable declaration is that a variable definition will allocate some space from memory to the variable and give it a initial value if you want, while a variable declaration does nothing but give just a declaration or an announcement.
Be careful when using global variable because it can be accessed and modified by any function through function side-effect, which provides an opportunity that what one function does to the global variable will disturb the smooth and correct implementation of other functions.