Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Pemrograman Berbasis Objek Johannes Simatupang, MKom, Cobit5 NIDN : 0318017001 www.opensolusi.info Pertemuan-4 Versi 2.0.1.

Presentasi serupa


Presentasi berjudul: "Pemrograman Berbasis Objek Johannes Simatupang, MKom, Cobit5 NIDN : 0318017001 www.opensolusi.info Pertemuan-4 Versi 2.0.1."— Transcript presentasi:

1 Pemrograman Berbasis Objek Johannes Simatupang, MKom, Cobit5 NIDN : 0318017001 www.opensolusi.info Pertemuan-4 Versi 2.0.1

2 Pokok Bahasan / Sub Pokok Bahasan Pertemuan-4

3 Modularity Programming (1): Function Concept  Functions membagi program ke dlm komponen program (modularisasi program) agar mudah di di-manage  Modules: functions dan classes  Programs dapat mengunakan modul baru dan “prepackaged” modules  Modul baru: programmer-defined functions, classes  Prepackaged: dari standard library (mis. Library Math)  Functions dipanggil oleh function call  Berupa function name dan argument  Analogy: Boss ke anak buah  Boss (calling function or caller) minta anak buahnya (called function) untuk melakukan task dan melaporkan (return) hasilnya. [A] Hal 162-166

4 Modularity Programming (2): Function Definition  Format function definition return-value-type function-name ( parameter-list ) { declarations and statements }  Return-value-type ▪ Data type hasil return ( void if nothing to returned)  Parameter list ▪ Deklarasi variable lokal yg dipisahkan oleh koma yg dikirim ke function ▪ Untuk mendapatkan informasi dari luar function ▪ If no arguments, use void or leave blank [A] Hal 162-166

5 Modularity Programming (3): Function Prototype  Memberitahukan compiler tentang tipe argument dan return type function int square( int ); Detail definisi fungsi ditulis nanti  Function prototype berisi  Function name  Parameters  Return type ( void if return nothing) [A] Hal 162-166

6 Modularity Programming (4): Function Prototype  Function Prototype harus sama dengan function definition  Function prototype double maximum( double, double, double );  Function Definition double maximum( double x, double y, double z ) { … }  Function signature  Adalah nama dan parameter function ▪ double maximum( double, double, double ); Function signature [A] Hal 162-170

7 Modularity Programming (5): Contoh Function Function Prototype Call Function Function Definition

8 Argumen Coercion  Arguments dapat memaksa type tertentu ▪ Converting int (4) to double (4.0) cout << sqrt(4)  Aturan konversi ▪ Argument secara otomatis di-convert ▪ Mengubah double menjadi int dapat memotong data 3.4 menjadi 3 ▪ Mixed type menjadi highest type (promotion) Int * double  double

9 Header Files  Header files berisi  Function prototypes  Definisi data types dan constant  Header files berakhiran.h  Programmer-defined header files #include “myheader.h”  Library header files #include

10 Recursion  Recursive functions  Functions that call themselves  Hanya dapat menyelesaikan base case  Jika tidak base case  Pecahkan problem ke dalam smaller problems  Buat function baru yg bekerja di smaller problem (recursive call/recursive step) ▪ Slowly converges towards base case ▪ Function makes call to itself inside the return statement

11 Contoh Recursion : Faktorial n! = n * ( n – 1 ) * ( n – 2 ) * … * 1  Recursive relationship  n! = n * ( n – 1 )! 5! = 5 * 4! 4! = 4 * 3!…  Base case  1! = 0! = 1 unsigned long factorial( unsigned long number ) { if ( number <= 1 ) // base case return 1; else return number * factorial( number - 1 ); }

12 Contoh Recursion : Deret Fibonacci  Deret Fibonacci fib(n) = fib(n-1) + fib(n-2) 0, 1, 1, 2, 3, 5, 8...  C++ code for Fibonacci function long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else return fibonacci( n - 1 ) + fibonacci( n – 2 ); }

13 Recursion vs. Iteration  Repetition  Iteration: explicit loop  Recursion: repeated function calls  Termination  Iteration: loop condition fails  Recursion: base case recognized  Keduanya melakukan pengulangan / looping

14 Inline Function  Inline functions  Keyword inline before function  Asks the compiler to copy code into program instead of making function call ▪ Reduce function-call overhead ▪ Compiler can ignore inline  Good for small and often-used functions  Example inline double cube( const double s ) { return s * s * s; }  const tells compiler that function does not modify s [A] Hal 195-166

15 Ilustrasi Inline Code

16 Contoh Inline Function #include const float Pi = 3.1415926; inline float area(const float r) {return Pi * r * r;} main() { float radius; std::cout << "Enter the radius of a circle: "; std::cin >> radius; std::cout << "The area is " << area(radius) << "\n"; return 0; }

17 Call Function Type  Call by value  Copy of data passed to function  Changes to copy do not change original  Prevent unwanted side effects  Call by reference  Function can directly access data  Changes affect original  Call by Pointer  copies the address of an argument into the formal parameter  Changes affect original [A] Hal 167-170

18 Ilustrasi Passing By Value

19 Passing Argument to Functions (1): Byvalue  Constant

20 Passing Argument to Functions (2): By Value  Variable

21 Reference parameter  Reference parameter  Alias for argument in function call ▪ Passes parameter by reference  Use & after data type in prototype ▪ void myFunction( int &data ) ▪ Read “ data is a reference to an int ”  Function call format the same ▪ However, original can now be changed [A] Hal 182-185

22 Ilustrasi Reference Parameter

23 Contoh Call By Reference

24 Function Overloading  Function overloading  Functions with same name and different parameters  Should perform similar tasks ▪ I.e., function to square int s and function to square float s int square( int x) {return x * x;} float square(float x) { return x * x; }  Overloaded functions dibedakan oleh signature  Based on name and parameter types (order matters)  Name mangling ▪ Encodes function identifier with parameters  Type-safe linkage ▪ Ensures proper overloaded function called [A] Hal 188-190

25 Default Arguments  Function call with omitted parameters  If not enough parameters, rightmost go to their defaults  Default values ▪ Can be constants, global variables, or function calls  Set defaults in function prototype int myFunction( int x = 1, int y = 2, int z = 3 );  myFunction(3) ▪ x = 3, y and z get defaults (rightmost)  myFunction(3, 5) ▪ x = 3, y = 5 and z gets default [A] Hal 197-198

26 See you next session ….


Download ppt "Pemrograman Berbasis Objek Johannes Simatupang, MKom, Cobit5 NIDN : 0318017001 www.opensolusi.info Pertemuan-4 Versi 2.0.1."

Presentasi serupa


Iklan oleh Google