Matakuliah
Tahun
Versi
: T0044/Pemrograman Berorientasi Obyek
: 2005
: 1.0
Pertemuan 2
Function & Pointer
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• menjelaskan prinsip kerja function
• menjelaskan prinsip kerja pointer
2
Outline Materi
•
•
•
•
•
•
•
Function Definitions
Function Prototypes
Recursion vs. Iteration
Inline Functions
Default Arguments
Function Overloading
Pointer Operator
3
Pendahuluan
• 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.
4
Mendefinisikan Function
• 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
5
Contoh Fungsi
int square( int y )
{
return y * y;
}
• keyword return
– Returns data, dan control ke function’s caller
• If no data to return, use return; atau void
pada return value type
6
Function Prototypes
• 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)
7
Function Prototypes
• 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
8
Argument 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 <cmath.h>
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 Rekursi: 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 Rekursi: 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
• Both can have infinite loops
14
Inline Functions
• 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
15
Call by value vs Call by reference
• 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
16
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
17
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
18
Function Overloading
• Function overloading
– Functions with same name and different parameters
– Should perform similar tasks
• I.e., function to square ints and function to square floats
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
19
DEKLARASI POINTER
• Variabel Pointer
– Contain memory addresses as values
– Normally, variable contains specific value (direct reference)
– Pointers contain address of variable that has specific value (indirect
reference)
• Indirection
– Referencing value through pointer
• Pointer declarations
– * indicates variable is pointer
int *myPtr;
declares pointer to int, pointer of type int *
– Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;
– Can declare pointers to any data type
• Pointer initialization
– Initialized to 0, NULL, or address
• 0 or NULL points to nothing
20
POINTER OPERATORS
• & (address operator)
– Returns memory address of its operand
– Example
int y = 5;
int *yPtr;
yPtr = &y;
// yPtr gets address of y
– yPtr “points to” y
yPtr
y
5
yptr
500000
600000
y
600000
5
address of y
is value of
yptr
21
POINTER OPERATORS
• * (indirection/dereferencing operator)
– Returns synonym for object its pointer
operand points to
– *yPtr returns y (because yPtr points to y).
– dereferenced pointer is lvalue
*yptr = 9;
// assigns 9 to y
• * and & are inverses of each other
22
Tugas 2
• Tugas 2A: Mahasiswa diminta membuat
contoh program dengan menggunakan
passing argumen by value & by reference
• Tugas 2B: Mahasiswa membuat function
overloading dengan 4 function prototype
yang berbeda
• Tugas 2C: Mahasiswa membuat analisis
dari satu contoh program pointer
23
© Copyright 2026 Paperzz