• C语言辅导班
• C++辅导班
• 数据结构辅导班
• 一对一答疑

# C++函数定义和调用过程（超详细）

• 名称：每个函数都必须有一个名称。通常，适用于变量名称的规则同样也适用于函数名称。
• 形参列表：调用函数的程序模块可以向其发送数据。形参列表是保存传递给函数的值的变量列表。如果没有值传递给函数，则其形参列表为空。
• 主体：函数的主体是处理函数正在执行的任务的一组语句。这些语句包含在一组大括号中。
• 返回类型：函数可以将值发送回调用它的程序模块。返回类型是要发送回的值的数据类型。 ## 空函数

```void displayMessage()
{
cout << "Hello from the function displayMessage.\n";
}```

## 调用函数

```// This program has two functions: main and displayMessage.
#include <iostream>
using namespace std;

// Function prototype
void displayMessage();
//mian函数
int main()
{
cout << "Hello from main.\n";
displayMessage(); // Call.displayMessage
cout << "Now we are back in the main function again. \n";
return 0;
}

void displayMessage()
{
cout << "Hello from the displayMessage function.\n";
}```

Hello from main.
Hello from the displayMessage function.
Now we are back in the main function again.

displayMessage();

• 函数头 -> void displayMessage；
• 函数调用-> displayMessage(); ```#include <iostream>
using namespace std;

// Function prototype
void displayMessage();

int main()
{
cout << "Hello from main.\n";
for (int count = 0; count < 3; count++)
{
displayMessage() ; // Call displayMessage
}
cout << "Back in function main again.\n";
return 0;
}
void displayMessage()
{
cout << "Hello from the function displayMessage.\n";
}```

Hello from main.
Hello from the displayMessage function.
Hello from the displayMessage function.
Hello from the displayMessage function.
Back in function main again.

```#include <iostream>
using namespace std;

// Function prototype
void deep();
void deeper();

int main()
{
cout << "工 am starting in function main.\n";
deep(); // Call function deep
cout << "Now I amback in function main again.\n";
return 0;
}

void deep()
{
cout << "I am now inside the function deep.\n";
deeper();    // Call function deeper
cout << "Now I am back in deep. \n";
}

void deeper()
{
cout << "I am now inside the function deeper.\n";
}```

I am starting in function main.
I am now inside the function deep.
I am now inside the function deeper.
Now I am back in deep.
Now I am back in function main again.
{"ft_lang_label":"__label__zh","ft_lang_prob":0.75742674,"math_prob":0.7437718,"size":3907,"snap":"2020-45-2020-50","text_gpt3_token_len":2047,"char_repetition_ratio":0.19779657,"word_repetition_ratio":0.2016129,"special_character_ratio":0.24852829,"punctuation_ratio":0.10600707,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95260894,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,2,null,2,null,2,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-12-01T06:40:51Z\",\"WARC-Record-ID\":\"<urn:uuid:03100d4c-2078-4ced-bf93-331d06ee8070>\",\"Content-Length\":\"20422\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:441082f1-b51e-4f10-9323-a7c950a0ea7c>\",\"WARC-Concurrent-To\":\"<urn:uuid:d6d6c8a9-3f01-4b1d-9491-170c502dcf13>\",\"WARC-IP-Address\":\"47.246.24.225\",\"WARC-Target-URI\":\"http://c.biancheng.net/view/1377.html\",\"WARC-Payload-Digest\":\"sha1:L57B5YSJ7MHCITZCJUJAKNXD35QLE7YA\",\"WARC-Block-Digest\":\"sha1:HUT6BGANUZJUABVEMGARCDJQCKNKSTTQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141652107.52_warc_CC-MAIN-20201201043603-20201201073603-00212.warc.gz\"}"}