# Mainpage
``` C++=
回傳型態 函式名稱 (引數)
{
函式內容
}
int add(int a, int b)
{
return a+b
}
```
```C++=
#include<iostream>
using namespace std;
int add(int, int);
int sub(int, int);
int mul(int, int);
int main()
{
char oper;
int num1, num2;
cout << "Please enter the operator" << endl;
cin >> oper;
cout << "Please enter the first number" << endl;
cin >> num1;
cout << "Please enter the second number" << endl;
cin >> num2;
switch(oper)
{
case '+':
cout << "Your final answer is:" << endl;
cout << add(num1,num2);
break;
case '-':
cout << "Your final answer is:" << endl;
cout << min(num1,num2);
break;
case '*':
cout << "Your final answer is:" << endl;
cout << mul(num1,num2);
break;
default:
cout << "Invalid operator" << endl;
break;
}
}
int add(int a, int b)
{
return a+b;
}
int sub(int a, int b)
{
return a-b;
}
int mul(int a, int b)
{
return a*b;
}
```
```C++=
#include<iostream>
using namespace std;
int main()
{
int lower;
int upper;
int temp = 0;
cout << "Please enter your lower and upper bound" << endl;
cin >> lower;
cin >> upper;
for(int i=lower; i<=upper; i++)
{
temp = temp + i;
}
cout << "The sum from the number" << " " << lower << " " << "to the number" << " " << upper << " "<< "is" << " " << temp;
return 0;
}
```
```C++=
#include<iostream>
using namespace std;
int main()
{
int upper;
int i;
int sum=0;
cout << "please enter your upper bound" << endl;
cin >> upper;
for(i=0; i<=upper; i++)
{
sum=sum+i;
}
cout << "1+2+3+....+" << upper << " = " << sum;
return 0;
}
```
```C++=
#include<iostream>
using namespace std;
int Z;
int main()
{
int X=10;
int Y=X*X;
Z = Y*Y;
cout << "X = " << X <<", Y = " << Y << " Z = " << Z <<endl;
}
```
```C++=
#include<iostream>
using namespace std;
double volume(int); //自訂函式雛形宣告
double surface(int); //自訂函式雛形宣告
const double pi = 3.14159; //宣告全域常數pi,所以函式都可以使用此常數
int main()
{
int r;
cout << "please enter the radius"<<endl;
cin >> r;
cout << "volume =" << volume(r) << " surface = " << surface(r) << endl;
return 0;
}
double volume(int r)
{
return (pi*r*r*r*4)/3;
}
double surface(int r)
{
return 4*pi*r*r;
}
```
```C++=
//file1.cpp
#include <iostream>
#include "file2.cpp"//使用雙引號將file2涵括進來
using namespace std;
extern void f(int);
int a; //宣告全域變數a
int main(void)
{
f(5);//呼叫函式f,將回傳值存進a
cout << "a=" << a <<endl;
return 0;
}
//file2.cpp
extern int a;
void f (int b)
{
a=b*b;
return;
}
```
```C++=
#include <iostream>
using namespace std;
int factorial(int n); //宣告自訂函式
int main()
{
int n;
cout << "Please enter a number" <<endl;
cin >> n;
cout << "The final result is " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*factorial(n-1); //這行敘述會在呼叫factorial一次
}
}
```
```C++=
#include <iostream>
using namespace std;
int main()
{
int i, n;
int temp = 1;
cout << "Input a number:";
cin >> n;
for(i=1; i<=n; i++)
{
temp = temp * i;
}
cout << temp;
return 0;
}
```
```C++=
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int i;
cout<<"argc = " <<argc<<endl;
for(i=0;i<argc;i++)
cout<<argv[i]<<endl;
return 0;
}
```
```C++=
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void game(int, int, int);
int main()
{
int answer;
srand(time(NULL));
answer = rand()%10000;
int left=1;
int right=10000;
int guess;
cout << "Please take a guess:"<<endl;
cin >> guess;
cout << "answer:"<<answer<<endl;
while(true)
{
if(answer<guess)
{
right = guess;
cout<< "Range = "<<left <<" to "<< right<<endl;
}
else if(answer>guess)
{
left = guess;
cout<< "Range = "<<left<<" to "<< right<<endl;
}
else if(answer==guess)
{
cout<< "You are right";
break;
}
cout << "Please take a guess:"<<endl;
cin >> guess;
}
return 0;
}
```
```C++=
#include<iostream>
using namespace std;
int C(int, int);
int main()
{
int n, r;
cout << "Calculate C(n, r), Please enter number of n and r"<< endl;
cin >> n >> r;
cout << C(n,r) <<endl;
return 0;
}
int C(int n, int r)
{
if(n < r || r < 0 || n < 0)
return -1;
else if(n == r || r == 0)
return 1;
else
return C(n-1,r) + C(n-1, r-1);
}
```
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
```C++=
v