Algorithms and Programming C/C++ 5
Object Oriented Programming
Object Oriented Programming (OOP) is a programming paradigm based on the concept of “objects.” For example, consider a remote-controlled toy car: the car itself is the object, and the remote control is the tool used to operate it. The RC car can be controlled to move forward, backward, left, and right through the remote, which illustrates the basic concept of object-oriented programming. Programming languages like C/C++ support OOP through features like functions.
Function
Before understanding functions, it’s helpful to revisit the concept of variables. Functions are similar to variables in that while a variable stores a value, a function stores a block of code. In C/C++, a function is a section of code designed to perform a specific task, separate from the program that calls it. In complex programs with multiple features, functions are essential for easier code readability and maintenance. With functions, there’s no need to write lengthy code in the main program; you can break it down into smaller parts and simply call the functions within the main program, thereby simplifying the main program’s structure.
Functions can have parameters to accept variables. When declaring parameters in a function, you can also include default arguments.
Functions can return values using the return keyword placed within the function’s code block to send a value back to the main program.
The main purpose of a function is to avoid code duplication, allowing code to be reused simply by calling the function’s name.
In C/C++, there are several important aspects to consider when declaring a function:
- Using the return keyword (for functions that return a value).
- Having function parameters/arguments.
- Supporting parameters with initial values/default parameters.
Generally, functions are categorized into two types:
- Functions that do not return a value.
- Functions that return a value.
Functions That Do Not Return a Value (Void Functions)
These functions are designed to contain a block of code for the purpose of reducing the number of lines in the program. They are typically called independently within a program block, as shown in the example below:
1 |
|
Functions That Return a Value
These functions return a specific value and are usually called and stored in a variable, as demonstrated below:
1 |
|
Program Blocks Inside Functions
After understanding functions, their categories, and their usage, the next step is to explore the content within the function’s code block. Before diving into the details of a function’s code block, it’s helpful to grasp a basic concept related to this. For example, if you have a debit/credit card with a VISA/MASTERCARD logo, you can use that card for transactions both nationally and internationally. However, if your debit/credit card does not have a VISA/MASTERCARD logo, you can only use it for national transactions. This concept of access and feature restrictions applies similarly to function code blocks, both inside and outside of functions. Once you understand this concept, we will introduce global and local variables.
Global Variables
Global variables can be used in any code block (inside function code blocks or outside function declarations). Because they are global, global variables can be accessed and used anywhere, whether in the same file or in different files.
Local Variables
Local variables can only be used within a specific function/class code block. Because of their limited scope, they can only be utilized within the block where they are declared and cannot be used outside of that function block.
Examples of Functions:
Function with parameters that returns a value:
1 |
|
Function with default parameters/initial values that does not return a value:
1 |
|
Recursive Function
A recursive function is a function that calls itself when declared. It is usually used as an alternative to loops, but it is rarely used because it consumes a significant amount of computer resources, causing the computer to work harder compared to using regular loop constructs.
Here is an example of a recursive function used to calculate factorials:
1 | factorial(5) = 5 * factorial(4) |
1 |
|
Function Exercise
Create a function that returns the results of addition, subtraction, division, multiplication, and modulus, with 2 parameters that have initial values (values can be arbitrary). Then, create a void function to print your name and where you are from at the top of the program. The program should run in a loop until the user decides to stop it. Here’s an example output:
OUTPUT:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16---------------------
| Azhar Rizki Zulma |
| Bintaro |
---------------------
Enter Menu (+|-|/|*|%|stop): + (Input by user)
Enter Value 1: 5 (Input by User)
Enter Value 2: 5 (Input by User)
The result of addition 5 + 5 is 10
Enter Menu (+|-|/|*|%|stop): - (Input by user)
Enter Value 1: 5 (Input by User)
Enter Value 2: 5 (Input by User)
The result of subtraction 5 - 5 is 0
Enter Menu (+|-|/|*|%|stop): stop (Input by user)
Program stopped. Thank you for using my program.