pengertian fungsi dalam C++ beserta contohnya (version english & indonesia)

| Kamis, 22 November 2012 | |

IN ENGLISH VERSION

The function is a block of instruction that is executed when called by other instructions in one line of the program elsewhere. The format of the function is as follows:
Type_data name_function ( type_data argument1, tipe_data argument2, ...)
{
Instruction;
Instruction;
.
.
.
}
data type is a data type that is returned by the function.

function name is the name of the function to be used when calling the function.
argument 1, argument 2. . . (as many as desired). Each argument consists of a data type followed by an identifier (name of the argument), declaring the argument just like variable declarations and acts as a variable in the function. This argument is used to make deliveries (passing) parameter to the function when the function is called. Separators between the arguments with the other argument is a comma.
instructions are after the opening brace and block the function ends before the final brace of the function block. Instructions instructions are collectively referred to as the body functions.
EXAMPLE:

// examp1efunciton1
#include <iostream.h>
 
int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}
 
int main ()
{
  int z;
  z = addition (5,3);
  cout << "result is : " << z;
  return 0;
}

To discuss the results of program execution function example 1 above, keep in mind that C + + always start the execution of the function main (). Main function will execute variable declarations Z, note that the variable Z of type integer, is the same type with the type of output functions of addition because the variable Z is prepared to accommodate the addition of of the function the results.





The parameters of the addition functions associated with the supplied parameters when calling function. So parameter A contains 5 and the parameter B contains 3.
At the moment the function addition is called from the main function, the program will move from the main function, leading to additional functions. The second value parameters passed to the function addition when calling it. Values ​​(5 and 3) be copied to a local variable in the function addition int a and int b.
And then execution in the command-line Function addition. Function addition declares a new variable (int r;). and then, command is executed r = a + b;. r will contain the value of a property plus the value of property b, which is 5 plus 3 result is 8, then r = 8.
Code of next program reads:
return (r);


This is the end of the addition function, then the execution of the program will return to the calling function point addition in the caller function is the main function. The order also states that the results issued by the addition to the caller function is the value of the R, in the above example is 8.





Value returned by the function addition will be accommodated by the caller, the call command above has been provided by the variable z to hold the results returned by the function addition. Because this instruction is the value of z-value 8.
The next program code:
cout << "the result is:  " << z;


The order is to be ordered display the string on the screen that reads:

The result is: 8

As you can see on your screen when the program is run.

Functions Without Output

Basic syntax of the functions mentioned above are:

Type_data name_ functions ( type_data argument1, tipe_data argument2, ...)
{
Instruction;
Instruction;
.
.
.
}


It can be seen that every function that is declared like this would give an output. What if we want to make the function but the function does not give output. The function that looks like will output during the execution of a return statement.
Suppose we want to create a function to display the message only screen when the input parameter = 0, the message "is zero", IF input parameter = 1 message: "This number ONE". We do not want to give the output of this function in any form.


// exampleFunctionVoid
#include <iostream.h>
 
void dummyfunction (int a)
{
  If a == 0  
    cout << "this number zero";
  else if a == 1 
    cout << "this number one";
}
 
int main ()
{
  dummyfunction (1);
  return 0;
}


Consider the example above, and watch the output and how dummyfunction function call (). In here you can see, because the function is not output it is not need to be provided a variable to hold the output function. So call in the main function quite reads: dummyfunction (1);
Void said no, so if a function no output then this function begins with the keyword void. So is the parameter, if the function does not require any parameters then in its declaration, in parentheses after the function name with the keyword void is filled. Writing this void optional, may not be written.

IN INDONESIA VERSION


Fungsi adalah sebuah blok instruksi yang dieksekusi ketika dipanggil oleh instruksi lain dalam salah satu baris program di tempat lain. Format daripada fungsi adalah sebagai berikut:
Tipe_data nama_fungsi ( tipe_data argument1, tipe_data argument2, ...)
{
Instruksi;
Instruksi;
.
.
.
}

 
· tipe_data adalah tipe_data yang akan dikembalikan oleh fungsi.
 
· nama_fungsi  adalah nama fungsi yang akan digunakan pada saat pemanggilan fungsi tersebut.
 
· argument1, argument2 . . . n  (sebanyak yang diinginkan). Setiap argumen terdiri dari tipe_data diikuti dengan identifier-nya (nama argumen), pedeklarasian argumen persis seperti deklarasi variabel (contoh, int x) dan bertindak seperti variabel didalam fungsi tersebut. Argumen – argumen ini digunkan untuk melakukan pengiriman (passing) parameter ke dalam fungsiketika fungsi tersebut dipanggil. Tanda pemisah antara satu argumen dengan argumen yang lain adalah tanda koma.
 
· instruksi berada setelah kurung kurawal pembukan blok fungsi dan berakhir sebelum kurung kurawal akhir dari blok fungsi. Instruksi instruksi ini secara bersama sama disebut sebagai badan fungsi.
      CONTOH :


// contohfungsi1
#include <iostream.h>
 
int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}
 
int main ()
{
  int z;
  z = addition (5,3);
  cout << "Hasilnya adalah : " << z;
  return 0;
}

Untuk membahas hasil eksekusi program contohfungsi1 diatas, perlu diingat bahwa C++ selalu memulai eksekusi dari fungsi main (). Fungsi main akan melaksanakan deklarasi variabel z, perhatikan bahwa variabel z bertipe integer, tipe ini sama dengan tipe dari keluaran fungsi addition karena variabel z disiapkan untuk menampung hasil dari fungsi penambahan.





Parameter parameter dari fungsi panambahan berhubungan dengan paramater yang disuplay pada saat pemanggilan fungsi. Jadi parameter a berisi 5 dan parameter b berisi 3.
Pada saat fungsi addition dipanggil dari fungsi main, program akan berpindah dari fungsi main, menuju ke fungsi penambahan. Nilai kedua parameters dikirimkan ke fungsi addition pada saat pemanggilan tersebut. Nilai (5 dan 3) di salin ke variabel lokal dalam fungsi addition int a dan int b.
Selanjutnya eksekusi dilakukan untuk perintah perintah di Function penambahan. Fungsi addition mendeklarasikan variabel baru (int r;). Selanjutnya dieksekusi perintah r=a+b;. r akan diisi nilai milik a ditambah nilai milik b, yaitu 5 plus 3 hasilnya adalah 8, maka r = 8.
Kode program berikutnya berbunyi:
return (r);

ini adalah akhir dari fungsi penambahan, selanjutnya eksekusi program akan kembali ke titik pemanggilan fungsi addition di fungsi pemanggil yaitu fungsi main. Perintah ini juga menyatakan bahwa hasil yang dikeluarkan oleh fungsi addition kepada pemanggilnya adalah nilai daripada r, dalam contoh diatas adalah 8.





Nilai yang di kembalikan oleh fungsi addition akan ditampung oleh pemanggilnya, dalam perintah pemanggilan diatas telah disediakan variabel z untuk menampung hasil yang dikembalikan oleh fungsi addition.   Oleh karena ini pada instruksi ini nilai z bernilai 8.
Kode program berikutnya adalah:
cout << "Hasilnya adalah :  " << z;

Peintah inilah yang akan memerintahkan menampilkan string di layar yang berbunyi :

Hasilnya adalah :  8

Seperti yang bisa anda saksikan di layar anda ketika program ini dijalankan.

Fungsi Tanpa Keluaran

Sintax dasar dari fungsi seperti disebutkan diatas adalah :
Tipe_data nama_fungsi ( tipe_data argument1, tipe_data argument2, ...)
{
Instruksi;
Instruksi;
.
.
.
}

Dapat dilihat disini bahwa setiap fungsi yang dideklarasikan seperti ini pasti memberikan keluaran. Bagaimana jika kita ingin membuat fungsi tetapi fungsi itu tidak memberikan keluaran (jika anda pernah belalar PASCAL ini disebut Procedure). Fungsi yang bentuknya seperti diatas akan memberikan keluaranya pada saat eksekusi pernyataan return.
Misalkan kita ingin membuat fungsi untuk menampilkan pesan dilayar saja ketika parameter masukan = 0, pesan “ini angka enol”, JIKA parameter masukan = 1 pesan : “ini angka SATU”. Kita tidak ingin memberikan keluaran fungsi ini dalam bentuk apapun.

// contohfungsiVoid

#include <iostream.h>
 
void dummyfunction (int a)
{
  If a == 0  
    cout << "ini angka ENOL";
  else if a == 1 
    cout << "ini angka SATU";
}
 
int main ()
{
  dummyfunction (1);
  return 0;
}


Perhatikan contoh diatas, dan perhatikan juga keluaran dan cara pemanggilan fungsi dummyfunction(). Disini dapat anda saksikan, oleh karena fungsi tersebut tidak ada keluarannya maka tidak perlu disediakan variabel untuk menampung keluaran fungsi ini. Maka pemanggilan di fungsi main cukup berbunyi :
dummyfunction (1);
Void menyatakan tidak ada, jadi jika sebuah fungsi tidak ada keluarannya maka fungsi ini diawali dengan keyword void. Begitu juga dengan parameter, jika sebuah fungsi tidak memerlukan parameter maka didalam deklarasinya, di dalam tanda kurung setelah nama fungsi diisi dengan keyword void.  Penulisan void ini optional, bisa saja tidak dituliskan. 

0 komentar:

Posting Komentar