1 / 11

Fungsi (1)

Fungsi (1). Dr. Anto Satriyo Nugroho, M.Eng Email: asnugroho@gmail.com Web: http://asnugroho.net/lecture/dp.html. Mengapa Fungsi penting dipelajari ?. Pemrograman modular dapat menyederhanakan masalah menjadi beberapa modul, sehingga lebih mudah disusun dan difahami

woody
Download Presentation

Fungsi (1)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fungsi (1) Dr. Anto Satriyo Nugroho, M.Eng Email: asnugroho@gmail.com Web: http://asnugroho.net/lecture/dp.html

  2. Mengapa Fungsi penting dipelajari ? • Pemrograman modular dapat menyederhanakan masalah menjadi beberapa modul, sehingga lebih mudah disusun dan difahami • Software reusability: fungsi yang pernah dibuat bisa dipakai di program lain

  3. Kategori dan Cara Penulisan Fungsi • Kategori Fungsi ada dua: • Standard Library Function • Programer-defined Function • Cara menulis Fungsi • Deklarasi fungsi • Definisi fungsi

  4. Kategori dan Cara Penulisan Fungsi • Deklarasi (function prototype) return_type function_name(type1,type2, …); • Definisi return_type function_name(parameter list) { deklarasi variabel_lokal; instruksi_1; … instruksi_n; return (value) }

  5. Contoh 1 #include <stdio.h> float lebihbesar(float f1,float f2) /* function definition */ { if(f1>f2) return f1; else return f2; } main() { float x,y; printf(“Bilangan ke-1: “); scanf(“%f”,&x); printf(“Bilangan ke-2: “); scanf(“%f”,&y); printf(“Nilai yang lebih besar adalah %f\n",lebihbesar(x,y)); }

  6. Contoh 1 float lebihbesar(float f1,float f2) { if(f1>f2) return f1; else return f2; } f1 dan f2 adalah parameter fungsi lebihbesar(x,y) x dan y adalah argumen

  7. Contoh 1 #include <stdio.h> main() { float x,y; printf(“Bilangan ke-1: “); scanf(“%f”,&x); printf(“Bilangan ke-2: “); scanf(“%f”,&y); printf(“Nilai yang lebih besar adalah %f\n",lebihbesar(x,y)); } float lebihbesar(float f1,float f2) { if(f1>f2) return f1; else return f2; } X

  8. Contoh 1 #include <stdio.h> float lebihbesar(float,float); main() { float x,y; printf(“Bilangan ke-1: “); scanf(“%f”,&x); printf(“Bilangan ke-2: “); scanf(“%f”,&y); printf(“Nilai yang lebih besar adalah %f\n",lebihbesar(x,y)); } float lebihbesar(float f1,float f2) { if(f1>f2) return f1; else return f2; }

  9. Quiz-1 : max untuk 3 bilangan #include <stdio.h> float lebihbesar(float,float,float); float lebihbesar(float f1,float f2,float f3) { } main() { float x,y,z; printf(“Bilangan ke-1: “); scanf(“%f”,&x); printf(“Bilangan ke-2: “); scanf(“%f”,&y); printf(“Bilangan ke-3: “); scanf(“%f”,&z); printf(“Nilai yang lebih besar adalah %f\n",lebihbesar(x,y,z)); } ?

  10. Quiz-2 : menghitung pangkat • #include <stdio.h> • double pangkat(double,int); • double pangkat(double dx, int no) • { • } • main() • { • int n; • double x; • printf(“Tulis sebarang bilangan riil: “); scanf(“%lf”,&x); • printf(“Tulis sebarang bilangan bulat: “); scanf(“%d”,&n); • printf(“%lf pangkat %d adalah %lf \n“,x,n,pangkat(x,n)); • } ?

  11. Array dan Fungsi #include <stdio.h> void matrix_jumlah(int x[2][3],int y[2][3],int row,int column) { int i,j; for (j=0;j<row;j++) { for(i=0;i<column;i++) { printf(“%d ”,x[j][i]+y[j][i]); } printf(“\n”); } } main() { int ma[2][3]= { {1, 2, 3}, {4, 5, 6} }; int mb[2][3]= { {6, 3, 4}, {5, 1, 2} }; matrix_jumlah(ma,mb,2,3); }

More Related