BP-4-2 Function

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;
}

  1. When executing main, the memory will allocate space to variable a, b and c;
  2. When calling function power, the memory will also allocate space to x, n, product and use a, b, 1.0 to initialize them;
  3. When function power ends its execution, x = 3.0; n = 0; product = 81.0;
  4. The return value 81.0 will be passed to c and the space of x, n, product will be released.

From the process above we can conclude that the arguments a and b 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值