-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
65 lines (51 loc) · 1.52 KB
/
functions.cpp
File metadata and controls
65 lines (51 loc) · 1.52 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
#include <iostream>
#include <string>
// Function declaration (prototype)
int& getReference(int& x);
void printMessage(std::string message = "Default message", int times = 1);
inline int multiply(int a, int b);
int max(int a, int b);
double max(double a, double b);
int max(int a, int b, int c);
int main() {
// Reference function
int value = 42;
int& ref = getReference(value);
ref = 100;
std::cout << "Modified value: " << value << std::endl;
// Default parameters
printMessage(); // Uses default parameters
printMessage("Custom message", 3); // Uses provided parameters
// Inline function
std::cout << "Product: " << multiply(4, 6) << std::endl;
// Function overloading
std::cout << "Max of 10, 20: " << max(10, 20) << std::endl;
std::cout << "Max of 3.5, 2.8: " << max(3.5, 2.8) << std::endl;
std::cout << "Max of 15, 25, 35: " << max(15, 25, 35) << std::endl;
return 0;
}
// Unique Function definitions
// Function returning reference
int& getReference(int& x) {
return x;
}
// Function with default parameters
void printMessage(std::string message, int times) {
for (int i = 0; i < times; i++) {
std::cout << message << std::endl;
}
}
// Inline function
inline int multiply(int a, int b) {
return a * b;
}
// Function overloading
int max(int a, int b) {
return (a > b) ? a : b;
}
double max(double a, double b) {
return (a > b) ? a : b;
}
int max(int a, int b, int c) {
return max(max(a, b), c);
}