The function is a block of program code designed to perform specific tasks. The function of many involved in the creation of a program, with the aim of:
The program became more tersetuktur, making it easy to understand and easy to develop
Can reduce code duplication.
The general form of a function is as follows:
Tipedata namafungsi (daftarparameter)
{
/ * Function Body * /
nilaireturn return; / * for the data type is not void * /
}
Functions of type void
Functions of type void, if the pascal or delphi program called procedure. This function has no return value, so the function of this type is just a bunch of code that works in accordance with the given parameters.
Examples of functions of type void:
TampilNama void ()
{
textcolor (RED);
cprintf ("My Name: Write name Ada \ n \ r");
cprintf ("Address: Jl. No. Can In Asphalt. 70 \ n \ r");
cprintf ("Phone: 022-2513709 \ n \ r");
}
main ()
{
TampilNama ();
TampilNama ();
TampilNama ();
}
In the above program, there is a function called TampilNama (), which is useful for displaying data Name, Address, and Phone. In the main program (function main ()), how to call a function is to write the name of the function (in this case TampilNama ()). So the above program will display the contents TampilNama function () 3 times.
The above function is a function called without using parameters. To see examples of parameterized functions, consider the program below.
void box (int X1, int Y1, int X2, int Y2, int frame, int background)
{
int i;
textcolor (Frame);
textbackground (Background);
gotoxy (X1, Y1); cprintf ("é") / * alt +218 * /
gotoxy (X1, Y2); cprintf ("% c", 192);
gotoxy (X2, Y1); cprintf ("% c", 191);
gotoxy (X2, Y2); cprintf ("% c", 217);
for (i = X1 +1; i <= X2-1; i + +)
{
gotoxy (i, Y1); cprintf ("% c", 196);
gotoxy (i, Y2); cprintf ("% c", 196);
}
for (i = Y1 +1; i <= Y2-1; i + +)
{
gotoxy (X1, i); cprintf ("% c", 179);
gotoxy (X2, i); cprintf ("% c", 179);
}
}
main ()
{
Box (1,1,80,24, WHITE, BLUE) ;/ / Calling Procedure Box
Box (2,2,15,23, WHITE, RED);
getch ();
return 0;
}
Void Box is a function that will create a box on the screen according to the coordinates given in the parameter. These coordinates are the top left coordinates (X1, Y1), and the coordinates of the lower right (X2, Y2). Besides this function requires a parameter that is useful for determining frame box frame color, as well as a useful parameter for determining background color background made box.
Dialling Box (1,1,80,24, WHITE, BLUE) allows you to create a box with upper left position on the coordinate (1,1) and bottom right position at coordinates (80.24) with the color white box frame with background blue box.
Tipedata namafungsi(daftarparameter)
{
/*Badan Fungsi*/
return nilaireturn; /* untuk tipe data bukan void */
}
|
void TampilNama()
{
textcolor(RED);
cprintf(“Nama Saya : Tulis Nama Ada\n\r”);
cprintf(“Alamat : Jl. Can Di Aspal No. 70\n\r”);
cprintf(“Telepon : 022-2513709\n\r”);
}
main()
{
TampilNama();
TampilNama();
TampilNama();
}
|
void Kotak(int X1,int Y1, int X2,int Y2,int Bingkai,int Latar)
{
int i;
textcolor(Bingkai);
textbackground(Latar);
gotoxy(X1,Y1);cprintf(“é”); /* alt+218 */
gotoxy(X1,Y2);cprintf(“%c”,192);
gotoxy(X2,Y1);cprintf(“%c”,191);
gotoxy(X2,Y2);cprintf(“%c”,217);
for (i=X1+1;i<=X2-1;i++)
{
gotoxy(i,Y1);cprintf(“%c”,196);
gotoxy(i,Y2);cprintf(“%c”,196);
}
for(i=Y1+1;i<=Y2-1;i++)
{
gotoxy(X1,i);cprintf(“%c”,179);
gotoxy(X2,i);cprintf(“%c”,179);
}
}
main()
{
Kotak(1,1,80,24,WHITE,BLUE);// Memanggil Procedur Kotak
Kotak(2,2,15,23,WHITE,RED);
getch();
return 0;
}
|
float F(float X)
{
return X*X+3*X+5;//Fungsi diisi hasil dari perhitungan X2+3*X+5
}
|
float Faktorial(int N)
{
int I;
float Hasil;
Hasil:=1;
for(I=2;I<=N;I++)
Hasil=Hasil * I;
return Hasil;
}
|
Float Kombinasi(int X, int Y)
{
return Faktorial(Y) / (Faktorial(X)*Faktorial(Y-X));
}
|
0 komentar:
Posting Komentar