C++ 函數(shù)

2018-03-24 16:44 更新

學(xué)習(xí)C++ - C++函數(shù)

以下代碼顯示如何創(chuàng)建函數(shù)。


#include <iostream> 
using namespace std; 
void my_function(int);    // function prototype for my_function() 

int main() { 
    my_function(3);       // call the my_function() function 
    cout << "Pick an integer: "; 
    int count; 
    cin >> count; 
    my_function(count);   // call it again 
    cout << "Done!" << endl; 
    return 0; 
} 

void my_function(int n)   // define the my_function() function 
{ 
    using namespace std; 
    cout << "Hi:" << n << " ." << endl; 
    // void functions don"t need return statements 
}

main()函數(shù)調(diào)用my_function()函數(shù)兩次,一次參數(shù)為3,一次為變量參數(shù)。

上面的代碼生成以下結(jié)果。

定義函數(shù)

沒(méi)有返回值的函數(shù)稱(chēng)為類(lèi)型void函數(shù),并具有以下一般形式:

void functionName(parameterList) { 
     statement(s) 
     return;          // optional 
} 

parameterList設(shè)置傳遞給函數(shù)的參數(shù)的類(lèi)型和數(shù)量。

可選的return語(yǔ)句標(biāo)記函數(shù)的結(jié)尾。

通常,您使用void函數(shù)執(zhí)行某種操作。

具有返回值的函數(shù)產(chǎn)生返回給調(diào)用者的值。

這樣的函數(shù)被聲明為具有與返回的值相同的類(lèi)型。這是一般的形式:

typeName functionName(parameterList) { 
   statements 
   return value;   // value is type cast to type typeName 
} 

函數(shù)原型

要使用C ++函數(shù),您必須提供函數(shù)定義,提供函數(shù)原型和調(diào)用函數(shù)。


#include <iostream>
using namespace std;
void simple();    // function prototype

int main()
{
    
    cout << "main() will call the simple() function:\n";
    simple();     // function call
  cout << "main() is finished with the simple() function.\n";
    return 0;
}
// function definition
void simple()
{
    cout << "a function.\n";
}

上面的代碼生成以下結(jié)果。

函數(shù)頭

列表2.5中的my_function()函數(shù)有這個(gè)頭:

void my_function(int n)

最初的void表示my_function()沒(méi)有返回值。

所以調(diào)用my_function()不會(huì)產(chǎn)生一個(gè)可以分配給main()中的變量的數(shù)字。

因此,第一個(gè)函數(shù)調(diào)用如下所示:

my_function(3);            // ok for void functions 

因?yàn)閙y_function()缺少一個(gè)返回值,所以你不能這樣使用:

simple = my_function(3);   // not allowed for void functions 

括號(hào)內(nèi)的int n表示您希望使用帶有int類(lèi)型的單個(gè)參數(shù)的my_function()。

n是一個(gè)新變量,分配了在函數(shù)調(diào)用期間傳遞的值。

以下函數(shù)調(diào)用將值3賦值給my_function()頭中定義的n變量:

my_function(3);

當(dāng)函數(shù)體中的cout語(yǔ)句使用n時(shí),它使用函數(shù)調(diào)用中傳遞的值。

具有返回值的用戶定義函數(shù)

以下代碼顯示如何轉(zhuǎn)換值。


#include <iostream> 

int convert(int);     // function prototype 
int main() { 
     using namespace std; 
     int input; 
     cout << "Enter the weight: "; 
     cin >> input; 
     int pounds = convert(input); 
     cout << input << " input = "; 
     cout << pounds << " pounds." << endl; 
     return 0; 
} 

int convert(int sts) 
{ 
      return 14 * sts; 
} 

在main()中,程序使用cin為整數(shù)變量輸入提供一個(gè)值。

此值作為參數(shù)傳遞給convert()函數(shù)。

上面的代碼生成以下結(jié)果。

在多功能程序中使用指令

要使std命名空間可用于這兩個(gè)函數(shù),請(qǐng)將指令放在兩個(gè)函數(shù)之外和之上:


#include <iostream> 
using namespace std; // affects all function definitions in this file 
void my_function(int); 

int main() { 
     my_function(3); 
     cout << "Pick an integer: "; 
     int count; 
     cin >> count; 
     my_function(count); 
     cout << "Done!" << endl; 
     return 0; 
} 

void my_function(int n) 
{ 
     cout << "my_function says touch your toes " << n << " times." << endl; 
} 

上面的代碼生成以下結(jié)果。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)