while loop使用關鍵字while,其意義就是讓部分程式碼在輸入參數為true的情況下重複執行。
#include
using namespace std;
int main()
{
int number;
cout << "Please input a positive integer..." << endl;
cin >> number;
int sum = 0;
int index = 1;
while(index <= number){
sum = sum + index;
index++;
}
cout << "The sum of 1+2+...+" << number << " = " << sum << endl;
return 0;
}
#include
using namespace std;
void f1(){
cout << "Execute Function 1" << endl;
}
double square(double x){
return x*x;
}
int main()
{
f1();
double d;
cout << "Please input a number:" << endl;
cin >> d;
cout << "The square of " << d << " is " << square(d) << endl;
return 0;
}
命名函數時也要遵循之前的命名規則,最好是有意義的字。。
若是輸入的引數超過一個,則使用逗點(,)分開。
#include
using namespace std;
int addition(int a, int b = 2){
return a+b;
}
int main()
{
int a, b;
cout << "Please input a number:" << endl;
cin >> a;
cout << "Please input another number:" << endl;
cin >> b;
cout << "The addition of " << a << " and " << b << " are " << addition(a,b) << endl;
return 0;
}
須注意有初始值的變數需置於沒有初始值的變數之後,也就是說(int a = 2, int b)是不行的,原因是因為若是我們呼叫addition(10),程式無法判斷10是要指派給a還是b。
上面的例子傳入到函數的引數都是傳值,若是要傳址(也就是兩者使用同一塊記憶體),可使用&符號。
#include
using namespace std;
int addition(int& a, int& b){
a = a*a;
b = b*b;
return a+b;
}
int main()
{
int a, b;
cout << "Please input a number:" << endl;
cin >> a;
cout << "Please input another number:" << endl;
cin >> b;
cout << "a = " << a << "\tb = " << b << endl;
cout << "The addition of " << a << " and " << b << " are " << addition(a,b) << endl;
cout << "NOW a = " << a << "\tb = " << b << endl;
return 0;
}
#include
using namespace std;
int addition(int a, int b);
int main()
{
int a, b;
cout << "Please input a number:" << endl;
cin >> a;
cout << "Please input another number:" << endl;
cin >> b;
cout << "The addition of " << a << " and " << b << " are " << addition(a, b) << endl;
return 0;
}
int addition(int a, int b){
return a+b;
}
#include
using namespace std;
/*
* Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, ...
* The index of first number(0) is 0, second number is 1......
*/
int fibonacci(int n){
if(n==0)
return 0;
else if(n==1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
int main()
{
int a;
cout << "Please input a number:" << endl;
cin >> a;
cout << "The " << a << "-th number of Fibonacci sequence is " << fibonacci(a) << endl;
for(int i = 0; i <= a; i++){
cout << fibonacci(i) << endl;
}
return 0;
}
#include
using namespace std;
int minimum(int& a, int& b){
if (a <= b)
return a;
else
return b;
}
int minimum(int& a, int& b, int& c){
if(c < minimum(a,b))
return c;
else
return minimum(a,b);
}
int main()
{
int a, b, c;
cout << "Please input a number:" << endl;
cin >> a;
cout << "Please input another number:" << endl;
cin >> b;
cout << "Please input another number:" << endl;
cin >> c;
cout << "The minimum of a,b is " << minimum(a,b) << endl;
cout << "The minimum of a,b,c is " << minimum(a,b,c) << endl;
return 0;
}
若是相同的計算但是不同的輸出入,可以使用template。
#include
using namespace std;
template
T sum (T a, T b){
T result = a+b;
return result;
}
int main()
{
cout << sum(5,6) << "\t" << sum(5.5, 6.6) << endl;
return 0;
}
#include
using namespace std;
int m = 10; // global variable
namespace ns1{
int m = 20; // variable in ns1
}
namespace ns2{
double k = 10.555; // variable in ns2
}
template
T multi_1(T m){
T result = m*ns1::m;
return result;
}
template
T multi_2(T a){
T result = a*m;
return result;
}
int main()
{
int m = 5; // local variable
cout << multi_1(m) << "\t" << multi_2(m) << "\t" << multi_2(ns1::m) << endl;
using namespace ns2;//using ns2::k;
cout << multi_1(k) << "\t" << multi_2(k) << endl;
return 0;
}