diff options
| author | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-07-10 22:24:20 +0200 |
| commit | 869e68986aa8f69af6e7842260a68d1e5c6f796f (patch) | |
| tree | 63b6b5ffc3d19414233d4629a533c0d9bf3cbf72 /CPP/Basics/functions.cpp | |
| parent | 20834dcc57537cd95260a4a22f5d91a027adfd35 (diff) | |
Add a bunch of code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'CPP/Basics/functions.cpp')
| -rw-r--r-- | CPP/Basics/functions.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/CPP/Basics/functions.cpp b/CPP/Basics/functions.cpp new file mode 100644 index 0000000..5a26992 --- /dev/null +++ b/CPP/Basics/functions.cpp @@ -0,0 +1,41 @@ +#include <iostream> + +// C++ can pass arguments are references + +// by value +int square(int x) +{ + return x * x; +} + +// swap by value (passing pointers) +void swap(int *x, int *y) +{ + int temp = *x; + *x = *y; + *y = temp; +} + +// Swap by reference - function overload +void swap(int& x, int& y) +{ + int temp = x; + x = y; + y = temp; +} + + +int main(void) { + + int a = 9, b; + b = square(a); + std::cout << "Square of a: " << b << std::endl; + std::cout << "A: " << a << " B: " << b << std::endl; + swap(&a, &b); + std::cout << "Swapping by addr value:" << std::endl; + std::cout << "New A: " << a << " New B: " << b << std::endl; + swap(a, b); + std::cout << "Swapping by reference:" << std::endl; + std::cout << "New A: " << a << " New B: " << b << std::endl; + return 0; +} |
