Monday, August 20, 2007

Mengenal function

Mengenal function

Function atau merupakan sejumlah pernyataan yang dikemas dalam sebuah nama. Nama ini selanjutnya dapat dipanggil berkali-kali di beberapa tempat pada program.
Tujuan penggunaan fungsi adalah:
- Memudahkan dalam mengembangkan program
- Menghemat ukuran program

Untuk membuat fungsi, harus mengikuti syntax sebagai berikut:

function namafungsi ($parameter1, $parameter2)
{
pernyataan1;
pernyataan2;
}

Contoh 1 : membuat fungsi yang tidak mempunyai parameter
Nama file : fungsi1.php

function BukaTabel()
{
echo " cellpadding=0 bgcolor=#555555>
\n";
echo " bgcolor=#ffffff>
\n";
echo "
";
}

function TutupTabel()
{
echo "

\n";
}
?>



Latihan Fungsi 1


BukaTabel();
print ("Ini tabel pertama");
TutupTabel();
print ("
");

BukaTabel();
print ("Ini tabel kedua");
TutupTabel();
?>

Contoh 2 : membuat fungsi yang mempunyai parameter
Nama file : fungsi2.php

function BukaTabel($warna1, $warna2)
{
echo " cellpadding=0 bgcolor=\"$warna1\">
\n";
echo " bgcolor=\"$warna2\">
\n";
echo "
";
}

function TutupTabel()
{
echo "

\n";
}
?>



Latihan Fungsi 2


BukaTabel("red", "#dddddd");
print ("Ini tabel pertama");
print ("

");
print ("");
print ("");
print ("");
print ("
Kolom 1 Kolom 2 Kolom 3
");
TutupTabel();
print ("
");
BukaTabel ("blue", "white");
print ("Ini tabel kedua");
TutupTabel();
?>

Download contoh

5 comments:

CresceNet said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my site, it is about the CresceNet, I hope you enjoy. The address is http://www.provedorcrescenet.com . A hug.

SBTVD said...

Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the SBTVD, I hope you enjoy. The address is http://sbtvd.blogspot.com. A hug.http://sbtvd.blogspot.com/

App Development Company London said...

Great article, Thanks for your great information, the content is quiet interesting. I will be waiting for your next post.

GST Consultants Delhi said...

Great post. All readers will definitely like this post. Looking forward for your next post.

App Developers London said...

Very interesting tips shared by you. I really like to read your blogs as they contain very important and informative content. I appreciate your efforts. Cheers