KAIHATSUGIKEN GROUP
********************************************************************************
10 引き数の値の変更
//関数display_valuesが終わってしまうと引き数の情報が破棄されて
//main関数の値に戻る
関数が終了してしまうと引き数に対して行った変更は破棄されてしまう
********
#include <iostream.h>
void display_values(int a, int b)
{
a = 1001;
b = 1001;
cout << "The vlues within display_values is are " <<
a << " and " << b << endl;
}
void main(void) //ここから始まる
{
int big = 2002, small = 0; //整数big,smallに値を代入
cout << "Values before function " << big << " and "
<< small << endl; //bigとsmallを代入表示
display_values (big, small); //void display_valueに値を渡す
cout << "Values after function " << big << " and "
<< amall << endl;
}
********
Values bifore vunction 2002 and 0
The values within display_values are 1001 and 1001
Values after function 2002 and 0
********
#include <iostream.h>
void display_values(int a, int b) //引き数int a,int bにbig smallの値を渡す
{
a = 1001; //引き数の値を1001に変更している
b = 1001; //引き数の値を1001に変更している
cout << "The vlues within display_values is are " <<
a << " and " << b << endl;//変更された値aとbをそれぞれに代入表示
} //ここから脱出してmainに戻る
void main(void)
{
int big = 2002, small = 0;
cout << "Values before function " << big << " and "
<< small << endl;
display_values (big, small);
cout << "Values after function " << big << " and "
<< amall << endl; //bigとsmallの値をここでさらに代入表示
} //関数display_valuesが終わってしまうと引き数の情報が破棄されて
//main関数の値に戻る
********
Values bifore vunction 2002 and 0
The values within display_values are 1001 and 1001
Values after function 2002 and 0
********************************************************************************
引き数値の変更
//アドレスとポインタを使うと関数が終了しても値は消去されずに残る
********
#include <iostream.h>
void change_values(int *a, int *b)
{
*a = 1001;
*b = 1001;
cout << "The values within display_values is are " <<
*a << " and " << *b << endl;
}
void main(void) //ここから始まる
{
int big = 2002, small = 0; //変数big,smallに値を代入
cout << "Values before function " << big << " and "
<< small << endl; //bigとsmallをそれぞれ代入する
change_values(&big, &small);//change_valuesの*aと*bにアドレスを引き渡す
cout << "Values after function " << big << " and "
<< small << endl;
}
********
Values before function 2002 and 0
The values within display_values are 1001 and 1001
Values agfter function 1001 and 1001
********
#include <iostream.h>
void change_values(int *a, int *b)//引き数int *a,int *bにbigとsmallの値が渡される
{
*a = 1001; //*a と *bの値がここで変更される
*b = 1001;
cout << "The values within display_values is are " <<
*a << " and " << *b << endl; //新しい値を持つ*aと*bを代入表示
} //新しく代入された値を持って脱出してmainへ戻る
void main(void)
{
int big = 2002, small = 0;
cout << "Values before function " << big << " and "
<< small << endl;
change_values(&big, &small);
cout << "Values after function " << big << " and "
<< small << endl;//値が変わったためにbigは1001 smallは1001になる
} //アドレスとポインタを使うと関数が終了しても値は消去されずに残る
********
Values before function 2002 and 0
The values within display_values are 1001 and 1001
Values agfter function 1001 and 1001
********