-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction+parameters.cpp
More file actions
112 lines (93 loc) · 3.26 KB
/
Copy pathfunction+parameters.cpp
File metadata and controls
112 lines (93 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
using namespace std;
// ===================================================
// 1. DECLARATION VS DEFINITION
// ===================================================
// Jika fungsi ditulis DI BAWAL main(), maka HARUS di-deklarasi dulu di atas.
void printMessage(); // Function Declaration (Prototype)
// Struct sederhana untuk demo Pass Structure
struct Car {
string brand;
int year;
};
// ===================================================
// 2. PARAMETERS & DEFAULT VALUES
// ===================================================
// Void = tidak mengembalikan nilai.
// Parameter dengan nilai default (`country = "Norway"`)
void greetUser(string fname, int age, string country = "Norway") {
cout << "Hello " << fname << " (" << age << " y/o) from " << country << endl;
}
// ===================================================
// 3. RETURN VALUES
// ===================================================
// Fungsi dengan tipe data pengembalian (int) & kata kunci `return`
int addNumbers(int x, int y) {
return x + y;
}
int doubleGame(int x) {
return x * 2;
}
// ===================================================
// 4. PASS BY REFERENCE (Penting!)
// ===================================================
// Menggunakan tanda `&`. Mengubah nilai variabel ASLI di main().
void changeValue(int &num) {
num = 50;
}
void swapNums(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
// ===================================================
// 5. PASS ARRAYS & STRUCTURES TO FUNCTIONS
// ===================================================
// Menerima Array sebagai parameter
void printArray(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << " ";
}
cout << endl;
}
// Pass Structure by Reference (`&c`) agar bisa mengubah data asli & hemat memori
void updateCarYear(Car &c) {
c.year++;
}
// ===================================================
// MAIN FUNCTION
// ===================================================
int main() {
cout << "=== 1. CALLING FUNCTION ===" << endl;
printMessage(); // Memanggil fungsi yang dideklarasikan di atas
cout << "\n=== 2. PARAMETERS & DEFAULT VALUES ===" << endl;
// "Liam" dan 20 adalah ARGUMENT
greetUser("Liam", 20, "Indonesia");
greetUser("Jenny", 14); // Menggunakan default country ("Norway")
cout << "\n=== 3. RETURN VALUES ===" << endl;
int result = addNumbers(5, 3);
cout << "5 + 3 = " << result << endl;
// Loop practical example
for (int i = 1; i <= 3; i++) {
cout << "Double of " << i << " is " << doubleGame(i) << endl;
}
cout << "\n=== 4. PASS BY REFERENCE ===" << endl;
int val = 10;
changeValue(val);
cout << "Value after changeValue(): " << val << endl; // Berubah jadi 50
int a = 10, b = 20;
swapNums(a, b);
cout << "After swap: a = " << a << ", b = " << b << endl;
cout << "\n=== 5. PASS ARRAY & STRUCT ===" << endl;
int myNums[5] = {10, 20, 30, 40, 50};
printArray(myNums); // Cukup panggil nama array-nya saja
Car myCar = {"Toyota", 2020};
updateCarYear(myCar);
cout << "Car: " << myCar.brand << ", Updated Year: " << myCar.year << endl;
return 0;
}
// Function Definition
void printMessage() {
cout << "I just got executed!" << endl;
}