函数:
返回值类型 函数名(参数列表) {
函数体
}
应在首次使用函数之前提供其原型,通常的做法是放到main函数前面。
#include <iostream> #include <cmath>//通过包含头文件来提供函数sqrt的原型 int main(void) { using namespace std; double area; cout << "Enter the floor area, in square feet, of your home: "; cin >> area; double side; side = sqrt(area); cout << "That's the equivalent of a square " << side << " feet to the side."; return 0; }
#include <iostream> void simon(const int n);//函数原型声明 int main(void) { simon(3); return 0; } void simon(const int n) {//函数定义 using namespace std; cout << "Simon says touch your toes " << n << " times." << endl; }
#include <iostream> int stonetolb(const int sts); int main(void) { using std::cout; using std::endl; using std::cin; int stone; cout << "Enter the weight in stone: "; cin >> stone; int pounds = stonetolb(stone); cout << stone << " stone = " << pounds << " pounds." << endl; return 0; } inline int stonetolb(const int sts) { return 14 * sts; }
1.C++程序的模块叫什么?
答:函数
2.下面的预处理器的作用是什么?
#include <iostream>
答:包含头文件iostream,将头文件内容添加到源代码中(与该行进行替换)
3.下面语句是做什么用的?
using namespace std;
答:using是预编译器指令,使用std名称空间
4.什么语句可以打印短语“Hello,world”,然后开始新的一行?
答:
cout << "Hello, world" << endl;
5.什么语句可以创建名称为cheeses的整数变量?
答:
int cheeses;
6.什么语句可以将32赋值给cheeses?
答:
cheeses = 32;
7.什么语句可以从键盘读值赋值给cheeses?
答:
cin >> cheeses;
8.什么语句可以打印“We have X varieties of cheese”,X为cheese的当前值
答:
cout << "We have " << cheeses << "varieties of cheese."
9.下面函数原型指出了关于函数的哪些信息?
int froop(double t);
答:函数返回值为int,函数名为froop,接受一个double类型的参数。
10.定义函数时,什么情况下不用写return?
答:函数返回类型为void
11.假设你写下如下代码并报错,请写出出现错误的可能原因并提出解决方案。
cout << “Please enter your PIN : ”;
答:1.原因:使用拼音符号 ;解决:改用英文符号
2.原因:未包含头文件iostream;解决:#include <iostream>
3.原因:未使用名称空间;解决:using namespace std;
1.编程一个c++程序,显示你的姓名和地址:
#include <iostream> int main(void) { std::cout << "Rick" << std::endl; std::cout << "I live in China." << std::endl; return 0; }
2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于220码).
#include <iostream> int main(void) { double distance; std::cout << "Enter the distance (in long): "; std::cin >> distance; std::cout << "The distance " << distance << " long" << " == " << 220 * distance << " yard"; return 0; }
3.编程一个C++程序,它使用用户自定义的两个函数,并生成以下的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中每个函数各调用两次
#include <iostream> void mice(); void run(); int main(void) { mice(); mice(); run(); run(); return 0; } inline void mice() { using namespace std; cout << "Three blind mice" << endl; } inline void run() { using namespace std; cout << "See how they run" << endl; }
4.编写一个程序,其中用户定义的函数以摄氏温度为参数,返回华氏温度。按照下列格式:
Please enter a Celsius value:20
20 degrees Celsius is 68 degrees Fahrenheit.
转换格式:华氏温度 = 1.8 x 摄氏温度 + 32.0
#include <iostream> double toFahreheit(double Celsius); int main(void) { using namespace std; double Celsius; cout << "Please enter a Celsius value: "; cin >> Celsius; cout << Celsius << " degrees Celsius is " << toFahreheit(Celsius) << " degrees Fahreheit."; return 0; } inline double toFahreheit(double Celsius) { return 1.8 * Celsius + 32.0; }
5.编写一个程序,要求用户分别输入小时数和分钟数。在main()函数中,将这两个只传递给一个void函数,后者以下面的格式显示:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include <iostream> void time(int hours, int minutes); int main(void) { int hours; std::cout << "Please enter the number of hours: "; std::cin >> hours; int minutes; std::cout << "Please enter the number of minutes: "; std::cin >> minutes; time(hours, minutes); return 0; } inline void time(int hours, int minutes) { std::cout << "Time: " << hours << ":" << minutes; }