You are on page 1of 18

FUNGSI

Return Value

Jika fungsi tidak bertipe void, maka fungsi akan


memberikan sebuah hasil.
Untuk mempunyai hasil maka didalam fungsi harus
terdapat perintah :
return <expression>;
Nilai yang diberikan oleh perintah return harus
mempunyai tipe yang sama dengan tipe fungsi.
Parameter Fungsi
Pass By Value

Pass By Reference
Pass by Value
Penyalinan nilai dari argumen fungsi
pemanggil ke parameter fungsi yang
dipanggil
argumen dari sebuah fungsi akan
mempunyai nilai seperti semula setelah
eksekusi fungsi.
Contoh 1
int sum(int num_1, int num_2) Meskipun nilai
{ parameter num_1
num_1 = num_1 + num_2; dirubah, argumen yang
return num_1; berhubungan yaitu
} var_x tidak berubah.

void main(){ Nilai dari argumen


disalin ke parameter,
int var_x, var_y, var_z; tetapi perubahan
var_x = 3; var_y = 5; parameter tidak
var_z = sum(var_x, var_y); mempengaruhi nilai
cout<<var_x<<var_y<< argumen.
var_z << endl;
Semua informasi dalam
}
variabel lokal akan
hilang ketika fungsi
berakhir.
Contoh 2
// Tes pengaruh fungsi pada parameternya
void Increment(int Number) {
Number = Number + 1;
cout << "The parameter Number is: " << Number
<< endl;
}

void main() {
int I= 10;

Increment(I); //argument variable


cout << "The variable I is: " <<I << endl;
Increment(35); //argument constant
cout << "The variable I is: " <<I<< endl;
Increment(I+26); //argument expression
cout << "The variable I is: "<<I<< endl;

}
Pass by Value: Contoh 2
Contoh 3
int feet(int);
int remain_inches(int);
void main() {
int inches; // Number of inches
cout << "Enter number of inches to convert:";
cin >> inches;
cout << "Result is " << feet(inches)
<< " feet " << remain_inches(inches)
<< " inches" << endl;
}

int feet(int input_inches) {


return input_inches / 12; }

int remain_inches(int input_inches) {


return input_inches % 12; }
pass-by-reference
Fungsi juga dapat didefinisikan dengan
melewatkan sebuah reference ke variable.
Hal ini dinamakan call-by-reference, fungsi dapat
merubah variabel pemanggil secara langsung.

Variabel reference adalah nama alternatif


nama untuk variable. Sebuah shortcut.

Variabel reference harus diinisialisasi ke


variabel lain.
Deklarasi Variable Reference
Untuk mendeklarasikan variable reference
diawali dengan tanda &:

int &foo;
double &blah;
char &c;
Contoh Variable Reference
int count;
int &b = count;
// b adalah variabel yang sama seperti
// count

count = 1;
cout << b is << b << endl;
b++;
cout << count is << count << endl;
Parameter Reference
Deklarasi parameter reference:

void add10( int &x) {


x = x+10; ce
ren
r efe
} ter
r ame
pa
add10(counter);
Passing Parameters by Reference

Supaya fungsi mempunyai multiple output, maka harus


menggunakan pass by reference.

Parameter harus menggunakan & untuk melakukan


passed by reference:
<type>& <variable>
contoh:
void Increment(int& Number);
void SumAve (double, double,double&,
double&);
Passing Parameters by Reference
Argumen yang berhubungan harus sebuah variable.
void Increment(int& Number);
void SumAve (double, double,double&, double&);

Increment(Inc);
SumAve (2.5, y+3, sum, mean);

Alamat (reference) dari variable dilewatkan ke function,


menggantikan nilainya.
Jika fungsi merubah nilai parameter, perubahan akan
direfeksikan dalam argumen yang berhubungan, karena
lokasi memorinya sama.
Contoh 1

void Increment(int& Number){


Number = Number + 1;
cout << "The parameter Number: "
<< Number << endl;
}
void main(){
int Inc = 10;
Increment(Inc); // parameter variable
cout << "The variable Inc is:
<<Inc<<endl;
}
Contoh 2
void SumAve (double, double, double&, double&);
int main ( ) {
double x, y, sum, mean;
cout << "Enter two numbers: ";
cin >> x >> y;
SumAve (x, y, sum, mean);
cout << "The sum is " << sum << endl;
cout << "The average is " << mean << endl;
return 0;
}
void SumAve(double no1, double no2, double&
sum, double& average) {
sum = no1 + no2;
average = sum / 2; }
Contoh 2
Contoh 3
void swap (int&, int&);
void main ( ) {
int first, second, third; // input integers

cin >> first >> second >> third;


if (first > second) swap (first, second);
if (second > third) swap (second, third);
if (first > second) swap (first, second);
cout << "The sorted integers are " << first
<<" , "<<second<<" , "<<third << endl;
}
void swap (int& num_1, int& num_2) {
int temp;
temp = num_1;
num_1 = num_2;
num_2 = temp;
}

You might also like