Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Functions (Bag 2). 2 Topik Scope Prototypes 3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses.

Similar presentations


Presentation on theme: "1 Functions (Bag 2). 2 Topik Scope Prototypes 3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses."— Presentation transcript:

1 1 Functions (Bag 2)

2 2 Topik Scope Prototypes

3 3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses pd saat fungsi sedang di eksekusi Buktinya, hal ini benar dari tiap blok dalam suatu program.

4 4 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c

5 5 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Deklarasi variabel Global

6 6 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Variables local pd block ini.

7 7 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Scope dr variable localVar.

8 8 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Scope dr variablel blockVar.

9 9 Prototyping Function Fungsi harus di deklarasikan sebelum di gunakan (spt variables) Deklarasi ini disebut “prototype” Menentukan nama, parameter dan return type dari function, tanpa badan fungsi.

10 10 Contoh: Continuing Fraction Diberikan n dan terms, hitung: y = 1 1+ 1+ 1 1+ 1 +1 n... Terms jml bentuk Pecahan yg di bagi

11 11 Contoh: Continuing Fraction Contoh: jika n adl 2, dan terms adl 1: y = 1+ 1 +1 n = 1+ 1 +1 2  1.33333

12 12 Fraction(n, terms) Contoh: Continuing Fraction Diberikan n dan terms, hitung: y = 1 1+ 1+ 1 1+ 1 +1 n...

13 13 Contoh: Continuing Fraction Sum(n, terms) 1 1+ 1 1+ 1 +1 n...

14 14 Fraction(n, terms - 1) Contoh: Continuing Fraction 1+ 1 1+ 1 +1 n...

15 15 Sum(n, 1) Contoh: Continuing Fraction 1 +1 n Term terakhir:

16 16 Contoh: cfraction.c Fraction ( n, terms ) { return 1 / Sum ( n, terms ) } hit pecahan berkelanjutan input n, terms set sum to 1 + Fraction(n, terms) output sum Sum ( n, terms ) { if ( last term ) then return 1 + n else return 1 + Fraction ( n, terms - 1 ) }

17 17 #include /*******************************\ hit pecahan berkelanjutan \*******************************/ double Fraction(double n, int terms); double Sum(double, int); int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } hit pecahan berkelanjutan input n, terms set sum to 1 + Fraction(n, terms) output sum Contoh: cfraction.c

18 18 #include /*******************************\ hit pecahan berkelanjutan \*******************************/ double Fraction(double n, int terms); double Sum(double, int); int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } hit pecahan berkelanjutan input n, terms set sum to 1 + Fraction(n, terms) output sum Contoh: cfraction.c Function prototypes

19 19 double Fraction(double n,int terms) { return (1 / Sum(n,terms)); } double Sum(double n, int terms) { if (terms==1) { return 1 + n; } else { return (1 + Fraction(n,terms-1)); } Fraction ( n, terms ) { return 1 / Sum ( n, terms ) } Sum ( n, terms ) { if ( last term ) { return 1 + n } else { return (1 + Fraction (n, terms-1)) } Contoh: cfraction.c

20 20 #include /*******************************\ hit pecahan berkelanjutan \*******************************/ double Fraction(double n, int terms); double Sum(double, int); int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } Header Files: Jika fungsi sering dipakai, prototipe fungsi biasanya disimpan dalam “header” file (file dg extensi.h )

21 21 double Fraction(double n, int terms); double Sum(double, int); funcs.h funcs.c double Fraction(double n,int terms) { return (1 / Sum(n,terms)); } double Sum(double n, int terms) { if (terms==1) { return 1 + n; } else { return (1 + Fraction(n,terms-1)); } Header Files:

22 22 #include #include “funcs.h” #include “funcs.c” int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } cfraction.c Header Files:

23 23 Berisi prototipe fungsi utk standard input/output. Standard header files biasanya disimpan dalam include directory. #include #include “funcs.h” #include “funcs.c” int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } Header Files:


Download ppt "1 Functions (Bag 2). 2 Topik Scope Prototypes 3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses."

Similar presentations


Ads by Google