KAIHATSUGIKEN GROUP
********************************************************************************
9 関数の基本
********
#include <iostream.h>
void show_message(void) //どんなときもmain関数から始まるのでここはとばされる
{
cout << "Hello,I've been Rescued by C++" << endl;
}
void main(void) //ここからスタートする
{
cout << "About to call the function" << endl;//まず画面表示する
show_message(); //ここで show_message() に戻る
cout << "Back from the functin" << endl;
}
********
c:\ show_msg <enter>
About to call the fuciton
Hello, I've been Rescued by C++
Back from the functin
********
#include <iostream.h>
void show_message(void)
{
cout << "Hello,I've been Rescued by C++" << endl;
//ここでさらに画面表示
}
void main(void)
{
cout << "About to call the function" << endl;
show_message(); //2回目の画面表示が終わったらまたここへ戻る
cout << "Back from the functin" << endl;//3回目の画面表示
}
********
c:\ show_msg <enter>
About to call the fuciton
Hello, I've been Rescued by C++
Back from the functin
********************************************************************************
関数への情報の伝達
********
#include <iostream.h>
void show_number(int value)
{
cout << "The parameter's value is " << value << endl;
}
void main(void) //nain から始まる
{
show_number(1); //show_numberとして 1を代入
show_number(1001); //show_numberとして1001を代入
show_number(-532); //show_numberとしてー532を代入
}
********
c:\> useparam <enter>
The parameter's value is 1
The parameter's value is 1001
The parameter's value is -532
********
#include <iostream.h>
void show_number(int value) //3つの関数の値が引き数valueに渡される
{
cout << "The parameter's value is " << value << endl;
//3つの関数の値を代入して
//を順番に表示
}
void main(void)
{
show_number(1);
show_number(1001);
show_number(-532);
}
********
c:\> useparam <enter>
The parameter's value is 1
The parameter's value is 1001
The parameter's value is -532
************************************************
#include <iostream.h>
void show_big_and_little(int a, int b, int c)
{
int small = a;
int big = a;
if ( b > big)
big = b;
if ( b < small)
small = b;
if ( c > big)
big = c;
if ( c < small)
small = c; small = 3
cout << "The biggest value is " << big << endl;
cout << "The smallest value is " << small << endl;
}
void main(void) //ここから始まる
{
show_big_and_little(1, 2, 3); //引き数int a, int b, int c,にそれぞれ代入
show_big_and_little(500, 0, -500);//引き数int a, int b, int c,にそれぞれ代入
show_big_and_little(1001, 1001, 1001); //引き数int a, int b, int c,にそれぞれ代入
}
********
c:\>bigsmall <enter>
The biggest value is 3
The smallest value is 1
The biggest value is 500
The smallest value is -500
The biggest value is 1001
The smallest value is 1001
********
#include <iostream.h>
void show_big_and_little(int a, int b, int c)//関数show_big_and_littleから引き数
//int a,int b, int cに値を渡す
{
int small = a; //a = 1
int big = a; //a = 1
if ( b > big) //2 > 1
big = b; // big = 2 正しい
if ( b < small) //2 < 1だから間違い
small = b; //small = 2
if ( c > big) //3 > 1
big = c; //big = 3 こちらの方がより大きいためこちらを実行
if ( c < small) //3 < 1間違いだから最初の代入値 int small = a = 1
small = c; //small = 3を実行する
cout << "The biggest value is " << big << endl; //big に3が代入
cout << "The smallest value is " << small << endl;
//small に1が代入
}
void main(void)
{
show_big_and_little(1, 2, 3);
show_big_and_little(500, 0, -500);
show_big_and_little(1001, 1001, 1001);
}
********
c:\>bigsmall <enter>
The biggest value is 3
The smallest value is 1
The biggest value is 500
The smallest value is -500
The biggest value is 1001
The smallest value is 1001
***********************************************
#include <iostream.h>
int add_values(int a, int b)
{
return( a + b );
}
void main(void) //ここから始まる
{
cout << "100 + 200 = " << add_values(100, 200) << endl;
//関数add_valuesの値100,200を変数add_valuesの引き数int a,int bへ値を渡す
cout << "500 + 501 = " << add_values(500, 501) << endl;
//関数add_valuesの値500,501を変数add_valuesの引き数int a,int bへ値を渡す
cout << " -1 + 1 = " << add_values(-1,1) << endl;
//関数add_valuesの値-1,1を変数add_valuesの引き数int a,int bへ値を渡す
}
*********
300
1001
0
*********
#include <iostream.h>
int add_values(int a, int b) //3種類それぞれの2つの値を引き数int a,int bに渡す
{
return( a + b ); //演算を実行して return で 実行終了
}
void main(void)
{
cout << "100 + 200 = " << add_values(100, 200) << endl;
cout << "500 + 501 = " << add_values(500, 501) << endl;
cout << " -1 + 1 = " << add_values(-1,1) << endl;
}
*********
300
1001
0
*********