《C++游戏编程入门》第5章 函数:Mad Lib
5.1 创建函数
Standard Template Library,提供算法、容器和迭代器等。
05.instructions.cpp
#include <iostream>
using namespace std;
// 函数原型 (声明,调用前)
void instructions();
int main()
{
instructions();//函数调用
return 0;
}
// 函数定义
void instructions()//函数头部
{
//函数体
cout << "Welcome to the most fun you've ever had with text!\n\n";
cout << "Here's how to play the game...\n";
}
5.2 使用形参和返回值
05.yes_or_no.cpp
#include <iostream>
#include <string>
using namespace std;
char askYesNo1(); // 指定返回类型char
char askYesNo2(string question); // 指定参数string类型
int main()
{
char answer1 = askYesNo1(); // 使用返回值
cout << "Thanks for answering: " << answer1 << "\n\n";
char answer2 = askYesNo2("Do you wish to save your game?"); // 形参传值
cout << "Thanks for answering: " << answer2 << "\n";
return 0;
}
char askYesNo1()
{
char response1;
do
{
cout << "Please enter 'y' or 'n': ";
cin >> response1;
} while (response1 != 'y' && response1 != 'n');
return response1; // return返回值
}
// 封装,隐藏细节,分开独立代码
char askYesNo2(string question)
{
char response2;
do
{
cout << question << " (y/n): "; // 使用形参
cin >> response2;
} while (response2 != 'y' && response2 != 'n');
return response2;
}
5.4 使用作用域
变量作用域决定可见范围。
05.scoping.cpp
#include <iostream>
using namespace std;
void func();
int main()
{
int var = 5; // local variable in main()
cout << "In main() var is: " << var << "\n\n";
func();
cout << "Back in main() var is: " << var << "\n\n";
{
// 嵌套作用域
cout << "In main() in a new scope var is: " << var << "\n\n";
cout << "Creating new var in new scope.\n";
int var = 10; // variable in new scope, hides other variable named var
cout << "In main() in a new scope var is: " << var << "\n\n";
}
cout << "At end of main() var created in new scope no longer exists.\n";
cout << "At end of main() var is: " << var << "\n";
for (int i = 0; i < 3; i++) // 循环外不存在
cout << i << endl;
return 0;
}
void func()
{
int var = -5; // 局部变量,函数外不可见
cout << "In func() var is: " << var << "\n\n";
}
5.5 使用全局变量
全局变量,程序各部分间共享信息(任意部分都能访问的变量)。
尽量少用。
05.global_reach.cpp
#include <iostream>
using namespace std;
int glob = 10; // 全局变量定义并初始化
void access_global();
void hide_global();
void change_global();
int main()
{
cout << "In main() glob is: " << glob << "\n\n"; // 访问全局变量
access_global();
hide_global();
cout << "In main() glob is: " << glob << "\n\n";
change_global();
cout << "In main() glob is: " << glob << "\n\n";
return 0;
}
void access_global()
{
cout << "In access_global() glob is: " << glob << "\n\n"; // 访问全局变量
}
void hide_global()
{
int glob = 0; // 隐藏全局变量
cout << "In hide_global() glob is: " << glob << "\n\n";
}
void change_global()
{
glob = -10; // 改变全局变量
cout << "In change_global() glob is: " << glob << "\n\n";
}
5.7 使用默认参数
为函数形参指定默认值。
05.give_me_a_number.cpp
#include <iostream>
#include <string>
using namespace std;
int askNumber(int high, int low = 1); // 函数声明中指定默认参数,默认参数放在最后面
int main()
{
int number = askNumber(5);
cout << "Thanks for entering: " << number << "\n\n";
number = askNumber(10, 5);
cout << "Thanks for entering: " << number << "\n\n";
return 0;
}
int askNumber(int high, int low)
{
int num;
do
{
cout << "Please enter a number"
<< " (" << low << " - " << high << "): ";
cin >> num;
} while (num > high || num < low);
return num;
}
5.8 函数重载
函数名相同,形参个数或类型不相同。
05.triple.cpp
#include <iostream>
#include <string>
using namespace std;
int triple(int number);
string triple(string text);
int main()
{
cout << "Tripling 5: " << triple(5) << "\n\n";
cout << "Tripling 'gamer': " << triple("gamer");
return 0;
}
int triple(int number)
{
return (number * 3);
}
string triple(string text)
{
return (text + text + text);
}
5.9 内联函数
短小函数,直接复制到调用处,程序不必跳转。
05.taking_damage.cpp
#include <iostream>
using namespace std;
int radiation(int health);//函数声明中不能使用inline
int main()
{
int health = 80;
cout << "Your health is " << health << "\n\n";
health = radiation(health);
cout << "After radiation exposure your health is " << health << "\n\n";
health = radiation(health);
cout << "After radiation exposure your health is " << health << "\n\n";
health = radiation(health);
cout << "After radiation exposure your health is " << health << "\n\n";
return 0;
}
inline int radiation(int health)
{
return (health / 2);
}
5.10 Mad Lib游戏
05.mad-lib.cpp
#include <iostream>
#include <string>
using namespace std;
string askText(string prompt);
int askNumber(string prompt);
void tellStory(string name, string noun, int number, string bodyPart, string verb);
int main()
{
cout << "Welcome to Mad Lib.\n\n";
cout << "Answer the following questions to help create a new story.\n";
string name = askText("Please enter a name: ");
string noun = askText("Please enter a plural noun: ");
int number = askNumber("Please enter a number: ");
string bodyPart = askText("Please enter a body part: ");
string verb = askText("Please enter a verb: ");
tellStory(name, noun, number, bodyPart, verb);
return 0;
}
string askText(string prompt)
{
string text;
cout << prompt;
cin >> text;
return text;
}
int askNumber(string prompt)
{
int num;
cout << prompt;
cin >> num;
return num;
}
void tellStory(string name, string noun, int number, string bodyPart, string verb)
{
cout << "\nHere's your story:\n";
cout << "The famous explorer ";
cout << name;
cout << " had nearly given up a life-long quest to find\n";
cout << "The Lost City of ";
cout << noun;
cout << " when one day, the ";
cout << noun;
cout << " found the explorer.\n";
cout << "Surrounded by ";
cout << number;
cout << " " << noun;
cout << ", a tear came to ";
cout << name << "'s ";
cout << bodyPart << ".\n";
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << noun << "\n";
cout << "promptly devoured ";
cout << name << ". ";
cout << "The moral of the story? Be careful what you ";
cout << verb;
cout << " for.";
}
文章评论