Chapter Review
- They is called functions.
- It causes the contents of the file to being substituted for this
iostream
directive before final compilation.
- It makes definitions made in the
std
namespace available to a program.
cout << "Hello, world" << endl;
Orcout << "Hello, world\n";
int cheeses;
cheeses = 32;
cin >> cheeses;
cout << "We have " << cheeses << " varieties of cheese\n";
- The function
froop()
expects to is called with one argument, which'll be double
is type, and that the function would return a T Ype int
value. For instance, it could is used as follows:
int gval = froop(3.14159);
The function has a rattle()
no return value and expects an int
argument. For instance, it could is used as follows:
rattle(37);
The function returns an and expects to is prune()
int
used without an argument. For instance, it could is used as follows:
int residue = prune();
- You do not have the-use
return
-a function when the function has the return type void
. However, you can use it if you don ' t give a return value:
return;
- The likely cause it, the function loses a statement to
using namespace std;
declare a directive of output.
Place using namespace std;
above the main()
function;
Place in the using std::cout;
main()
function;
Type as following:using std::cout << "Please enter your PIN: " << std::endl;
Programming Exercises1
#include <iostream>int main(){ usingnamespace std; "Name: NaRiSu;\n" "Address: BeiJing;" << endl; return0;}
2
#include <iostream>int main(){ usingnamespace std; int furlong; cin >> furlong; " furlong(s) = " 220" yard(s)" << endl; return0;}
3
#include <iostream>void f1();void f2();usingnamespace std;int main(){ f1(); f1(); f2(); f2(); return0;}void f1(){ "Three blind mice" << endl;}void f2(){ "See how they run\n"// 也可以用f1()函数中的endl形式。 return// void f2()表明无返回值,故可以这样写;也可以如f1()不写return语句。}
4
#include <iostream>int main(){ usingnamespace std; "Enter your age: "; int age; cin >> age; "Your age in months is "12"." << endl; return0;}
5
#include <iostream>double fahrenheit(double);int main(){ usingnamespace std; "Please enter a Celsius value: "; int celsius; cin >> celsius; " degrees Celsius is " " degrees Fahrenheit." << endl; return0;}double fahrenheit(double n){ return1.832.0;}
6
#include <iostream>double astronomical(double);int main(){ usingnamespace std; "Enter the number of light years: "; double light; cin >> light; " light years = " " astronomical units." << endl; return0;}double astronomical(double n){ return63240 * n;}
7
#include <iostream> void F (int , int ); int Main () {using namespace std; cout << "Enter the number of Hours:" ; int hour; CIN >> Hour; cout << "Enter the number of minutes:" ; int minute; CIN >> minute; F (hour, minute); return 0 ;} void F (int H, int m) { Using namespace std; cout << "time:" << H << << m << Endl; }
C + + Primer plus the sixth edition of the English version Chapter 2