Learning C++之 1.4 函數和返回值

函數是一個可以實現特定功能不斷重複使用的一系列語句。前面已經說過每個程序都必須有一個主函數:main()。然而多數程序都用到了很多個函數。

通常來說,你的程序有時候在運行的時候可能需要中斷一下去做其他事情。你在生活中也常常遇到這些事情,舉個例子。當你讀書時,你接到了一個電話。這個時候你就需要對書做一個標記,然後去接電話,接完電話後再去接着讀書。

C++程序也是如此,程序都是按順序執行着,知道遇到一個函數調用。函數調用是一個標記,道速CPU暫時中斷執行現有程序,切換到另一個函數繼續執行。CPU會標記一下現在執行的地點,然後去調用相應的函數。當調用的函數執行完成以後,程序再從標記的地方繼續執行。

啓動函數調用的函數稱作調用方,被調用的函數叫做被調用方。下面是一個例子:

//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream> // for std::cout and std::endl
 
// Definition of function doPrint()
void doPrint() // doPrint() is the called function in this example
{
    std::cout << "In doPrint()" << std::endl;
}
 
// Definition of function main()
int main()
{
    std::cout << "Starting main()" << std::endl;
    doPrint(); // Interrupt main() by making a function call to doPrint().  main() is the caller.
    std::cout << "Ending main()" << std::endl;
 
    return 0;
}

上面的函數輸出是:

Starting main()

In doPrint()

Ending main()

該函數的執行過程如下:首先執行main()函數,直到doPrint,此時CPU會暫停main函數的執行,並做標記,去執行doPrint()函數。該函數執行完成之後,再次從中斷前標記的地方執行main()函數。

你可以看到函數的調用就是調用函數的名稱,例子中的函數並沒有參數,帶有參數的函數後面會講到。這裏一定要記住,不要忘了括號。

返回值:

如上面的例子當主函數停止執行的時候,會返回一個0給系統,這個0就是返回值。

當你寫自己的程序的時候,返回值可以有,也可以沒有,根據函數的功能所定。這個可以通過在函數名稱前增加一個數據類型來做,如果沒有就用void,有就可用int,char等等。這個類型並不代表一定返回某個特定的值,而是這種類型的任意值都可以。

然後我們在函數裏面使用return語句來返回相應的數據類型給調用方,如下面的例子:

// int means the function returns an integer value to the caller
int return5()
{
    // this function returns an integer, so a return statement is needed
    return 5; // we're going to return integer value 5 back to the caller of this function
}
 
int main()
{
    std::cout << return5() << std::endl; // prints 5
    std::cout << return5() + 2 << std::endl; // prints 7
 
    return5(); // okay: the value 5 is returned, but is ignored since main() doesn't do anything with it
 
    return 0;
}

函數int return5()的返回值類型是int類型,通過return語句也返回來int類型數據:5.

返回值爲void類型

有時候函數不需要返回具體的值,這個時候就需要通過void類型來告訴調用者,該函數不反悔任何具體的值。如:

void doPrint() // void is the return type
{
    std::cout << "In doPrint()" << std::endl;
    // This function does not return a value so no return statement is needed
}

因爲沒有具體的返回值,也就不需要return語句了。

參考下面的例子:

// void means the function does not return a value to the caller
void returnNothing()
{
    std::cout << "Hi" << std::endl;
    // This function does not return a value so no return statement is needed
}
 
int main()
{
    returnNothing(); // okay: function returnNothing() is called, no value is returned
 
    std::cout << returnNothing(); // error: this line will not compile.  You'll need to comment it out to continue.
 
    return 0;
}

在第一次調用的時候,函數將會正常調用returnNothing,打印Hi。但是第二次調用的時候編譯器會報錯。因爲我們的returnNothing函數不會返回任何值。但是std::cout需要輸出值到屏幕上,這就編譯報錯了。

主函數的返回:

你現在應該有一個簡單的概念main()函數是如何執行的。當程序執行的時候,首先跑到main函數中執行。main函數執行完成之後,都會返回一個0給系統,所以我們命名是:int main()。

爲什麼main()函數要返回0呢,這個0稱作狀態碼。告訴系統程序的運行狀態,如果是0表示程序運行成功。如果是其他值,標識程序出問題了。

C++中定義了main必須有一個int類型的返回值。所以建議你在寫main()函數的時候,需要默認寫爲int main()。

同時建議將main()函數寫在程序的最下面,這個我們會在後面將爲什麼。

關於返回值一些其他的注意事項:

首先如果你定義一個函數是有返回值的,那麼必須使用return語句返回相應類型的值。main函數除外,因爲可能main函數你不返回的話,系統會默認給你返回0.

其次,你需要知道一旦調用return語句,那麼該函數就結束了,任何之後的多餘代碼都不會執行。

再次,一個函數只能返回一個值。函數可能會使用自己的邏輯控制決定返回哪一個值。函數可以返回一個值,一個變量或者一個聲明,或者 會從一堆可能的值中選擇其中的一個返回。

當然想要從函數中獲取多個值的方案也是可行的,這個我們會在後面進行深入的探討。

最後,返回值可以任意地定義成你需要的樣子。一些函數用返回值來返回程序的狀態碼,一些用來返回計算的結果,一些不用返回值。因爲返回值多種多樣,所以建議寫程序的時候最好備註一下返回值的意思。

複用的函數:

一些函數在程序裏會重複使用多次,當然這也是爲什麼把這部分抽象成函數的原因。

//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
 
// getValueFromUser will read a value in from the user, and return it to the caller
int getValueFromUser()
{
    std::cout << "Enter an integer: "; // ask user for an integer
    int a; // allocate a variable to hold the user input
    std::cin >> a; // get user input from console and store in variable a
    return a; // return this value to the function's caller (main)
}
 
int main()
{
    int x = getValueFromUser(); // first call to getValueFromUser
    int y = getValueFromUser(); // second call to getValueFromUser
 
    std::cout << x << " + " << y << " = " << x + y << std::endl;
 
    return 0;
}

這個函數就使用了getValueFromUser兩次,目的是輸入值給變量。需要注意的是不只main函數可以調用其他函數,任意的函數都可以調用其他函數,如下面的例子:

//#include "stdafx.h" // Visual Studio users need to uncomment this line
#include <iostream>
 
void printA()
{
    std::cout << "A" << std::endl;
}
 
void printB()
{
    std::cout << "B" << std::endl;
}
 
// function printAB() calls both printA() and printB()
void printAB()
{
    printA();
    printB();
}
 
// Definition of main()
int main()
{
    std::cout << "Starting main()" << std::endl;
    printAB();
    std::cout << "Ending main()" << std::endl;
    return 0;
}
嵌套函數:

在C++裏面,函數不能在另外一個函數裏面定義,也就是嵌套定義。下面的例子是非法的:

#include <iostream>
 
int main()
{
    int foo() // this function is nested inside main(), which is illegal.
    {
        std::cout << "foo!" << std::endl;
        return 0;
    }
 
    foo();
    return 0;
}

正確地寫法如下:

#include <iostream>
 
int foo() // no longer inside of main()
{
    std::cout << "foo!" << std::endl;
    return 0;
}
 
int main()
{
    foo();
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章