傳回值型態 函數名稱(struct 結構名稱);
函數定義
傳回值型態 函數名稱(struct 結構名稱1 變數1){
函數呼叫
函數名稱(結構名稱);
[code language=”cpp”]
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
string id;
int math;
int eng;
};
void show(struct student); //函數原型宣告
int main() {
struct student s1={"Tony","AMA104143",70,60}; //建立物件並且初始化
show(s1); //呼叫函數,印出所有欄位
}
//函數定義
void show(struct student a){
cout << "name=" << a.name << endl;
cout << "id=" << a.id << endl;
cout << "math=" << a.math << endl;
cout << "english=" << a.eng << endl;
}
[/code]
傳回值型態 函數名稱(型態1,型態2……);
函數定義
傳回值型態 函數名稱(型態1 引數1 ,型態2 引數2……){
函數呼叫
函數名稱(引數1 ,引數2……);
[code language=”cpp”]
#include <iostream>
#include <string>
using namespace std;
struct student
{
string name;
string id;
int math;
int eng;
};
void sum(int,int); //函數原型宣告
int main() {
struct student s1={"Tony","AMA104143",70,60}; //建立物件並且初始化
sum(s1.math,s1.eng); //呼叫函數,算出總和
}
//函數定義
void sum(int a,int b){
int sum=0;
sum=a+b;
cout << "sum=" << sum <<span style="display: inline-block; width: 0px; overflow: hidden; line-height: 0;" data-mce-type="bookmark" class="mce_SELRES_start"></span>< endl;
}
[/code]