c++的学习!

来源:互联网 发布:淘宝家装论坛 编辑:程序博客网 时间:2024/05/29 17:50

function is a sequence of statements designed to do a particular job. You already know that every program must have a function named main(). However, most programs have many functions, and they all work analogously to main.

Often, your program needs to interrupt what it is doing to temporarily do something else. You do this in real life all the time. For example, you might be reading a book when you remember you need to make a phone call. You put a bookmark in your book, make the phone call, and when you are done with the phone call, you return to your book where you left off.

C++ programs work the same way. A program will be executing statements sequentially inside one function when it encounters a function call. A function call is an expression that tells the CPU to interrupt the current function and execute another function. The CPU “puts a bookmark” at the current point of execution, and then calls (executes) the function named in the function call. When the called function terminates, the CPU goes back to the point it bookmarked, and resumes execution.

Here is a sample program that shows how new functions are declared and called:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
 
// Declaration of function DoPrint()
voidDoPrint()
{
    usingnamespace std;  // we need this in each function that uses cout and endl
    cout << "In DoPrint()" << endl;
}
 
// Declaration of main()
intmain()
{
    usingnamespace std;  // we need this in each function that uses cout and endl
    cout << "Starting main()" << endl;
    DoPrint();// This is a function call to DoPrint()
    cout << "Ending main()" << endl;
    return0;
}

This program produces the following output:

Starting main()In DoPrint()Ending main()

This program begins execution at the top of main(), and the first line to be executed prints Starting main(). The second line in main is a function call to DoPrint. At this point, execution of statements in main() is suspended, and the CPU jumps to DoPrint(). The first (and only) line in DoPrint prints In DoPrint(). When DoPrint() terminates, the caller (main()) resumes execution where it left off. Consequently, the next statment executed in main prints Ending main().

Functions can be called multiple times:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
 
// Declaration of function DoPrint()
voidDoPrint()
{
    usingnamespace std;
    cout << "In DoPrint()" << endl;
}
 
// Declaration of main()
intmain()
{
    usingnamespace std;
    cout << "Starting main()" << endl;
    DoPrint();// This is a function call to DoPrint()
    DoPrint();// This is a function call to DoPrint()
    DoPrint();// This is a function call to DoPrint()
    cout << "Ending main()" << endl;
    return0;
}

This program produces the following output:

Starting main()In DoPrint()In DoPrint()In DoPrint()Ending main()

In this case, main() is interrupted 3 times, once for each call to DoPrint().

Main isn’t the only function that can call other functions. In the following example, DoPrint() calls a second function, DoPrint2().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
 
voidDoPrint2()
{
    usingnamespace std;
    cout << "In DoPrint2()" << endl;
}
 
// Declaration of function DoPrint()
voidDoPrint()
{
    usingnamespace std;
    cout << "Starting DoPrint()" << endl;
    DoPrint2();// This is a function call to DoPrint2()
    DoPrint2();// This is a function call to DoPrint2()
    cout << "Ending DoPrint()" << endl;
}
 
// Declaration of main()
intmain()
{
    usingnamespace std;
    cout << "Starting main()" << endl;
    DoPrint();// This is a function call to DoPrint()
    cout << "Ending main()" << endl;
    return0;
}

This program produces the following output:

Starting main()Starting DoPrint()In DoPrint2()In DoPrint2()Ending DoPrint()Ending main()

Return values

If you remember, when main finishes executing, it returns a value back to the operating system (the caller) by using a return statement. Functions you write can return a single value to their caller as well. We do this by changing the return type of the function in the function’s declaration. A return type ofvoid means the function does not return a value. A return type of int means the function returns an integer value to the caller.

1
2
3
4
5
6
7
8
9
10
11
// void means the function does not return a value to the caller
voidReturnNothing()
{
    // This function does not return a value
}
 
// int means the function returns an integer value to the caller
intReturn5()
{
    return5;
}

Let’s use these functions in a program:

1
2
3
cout << Return5(); // prints 5
cout << Return5() + 2; // prints 7
cout << ReturnNothing(); // This will not compile

In the first statement, Return5() is executed. The function returns the value of 5 back to the caller, which passes that value to cout.

In the second statement, Return5() is executed and returns the value of 5 back to the caller. The expression 5 + 2 is then evaluated to 7. The value of 7 is passed to cout.

In the third statement, ReturnNothing() returns void. It is not valid to pass void to cout, and the compiler will give you an error when you try to compile this line.

One commonly asked question is, “Can my function return multiple values using a return statement?”. The answer is no. Functions can only return a single value using a return statement. However, there are ways to work around the issue, which we will discuss when we get into the in-depth section on functions.

Returning to main

You now have the conceptual tools to understand how the main() function actually works. When the program is executed, the operating system makes a function call to main(). Execution then jumps to the top of main. The statements in main are executed sequentially. Finally, main returns a integer value (usually 0) back to the operating system. This is why main is declared as int main().

Some compilers will let you get away with declaring main as void main(). Technically this is illegal. When these compilers see void main(), they interpret it as:

1
2
3
4
5
intmain()
{
    // your code here
    return0;
}

You should always declare main as returning an int and your main function should return 0 (or another integer if there was an error).

Parameters

In the return values subsection, you learned that a function can return a value back to the caller.Parameters are used to allow the caller to pass information to a function! This allows functions to be written to perform generic tasks without having to worry about the specific values used, and leaves the exact values of the variables up to the caller.

This is a case that is best learned by example. Here is an example of a very simple function that adds two numbers together and returns the result to the caller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
 
// add takes two integers as parameters, and returns the result of their sum
// add does not care what the exact values of x and y are
intadd(intx, inty)
{
    returnx + y;
}
 
intmain()
{
    usingnamespace std;
    // It is the caller of add() that decides the exact values of x and y
    cout << add(4, 5) << endl; // x=4 and y=5 are the parameters
    return0;
}

When function add() is called, x is assigned the value 4, and y is assigned the value 5. The function evaluates x + y, which is the value 9, and then returns this value to the caller. This value of 9 is then sent to cout to be printed on the screen.

Output:

9

Let’s take a look at a couple of other calls to functions():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//#include <stdafx.h> // Visual Studio users need to uncomment this line
#include <iostream>
 
intadd(intx, inty)
{
    returnx + y;
}
 
intmultiply(intz, intw)
{
    returnz * w;
}
 
intmain()
{
    usingnamespace std;
    cout << add(4, 5) << endl; // evalutes 4 + 5
    cout << add(3, 6) << endl; // evalues 3 + 6
    cout << add(1, 8) << endl; // evalues 1 + 8
 
    inta = 3;
    intb = 5;
    cout << add(a, b) << endl; // evaluates 3 + 5
 
    cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3)
    cout << add(1, add(2, 3)) << endl; // evalues 1 + (2 + 3)
    return0;
}

This program produces the output:

999876

The first three statements are straightforward.

The fourth is relatively easy as well:

1
2
3
inta = 3;
intb = 5;
cout << add(a, b) << endl; // evaluates 3 + 5

In this case, add() is called where x = a and y = b. Since a = 3 and b = 5, add(a, b) = add(3, 5), which resolves to 8.

Let’s take a look at the first tricky statement in the bunch:

1
cout << add(1, multiply(2, 3)) << endl; // evalues 1 + (2 * 3)

When the CPU tries to call function add(), it assigns x = 1, and y = multiply(2, 3). y is not an integer, it is a function call that needs to be resolved. So before the CPU calls add(), it calls multiply() where z = 2 and w = 3. multiply(2, 3) produces the value of 6, which is assigned to add()’s parameter y. Since x = 1 and y = 6, add(1, 6) is called, which evaluates to 7. The value of 7 is passed to cout.

Or, less verbosely (where the => symbol is used to represent evaluation):
add(1, multiply(2, 3)) => add(1, 6) => 7

The following statement looks tricky because one of the parameters given to add() is another call to add().


0 0