Algorithms and Programming Python 5
Object Oriented Programming I
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 device that controls it. You can move the RC car forward, backward, left, and right using the remote, illustrating the basic concept of OOP. Python supports Object Oriented Programming through features like functions and classes.
Definition (Function)
Before delving into functions, it’s helpful to revisit the concept of variables. While variables are used to store values, functions are used to store blocks of code. A function in Python is a section of code designed to perform a specific task, separate from the main program that calls it. In complex programs with many features, using functions is essential. Functions simplify code readability and maintenance, allowing us to avoid writing lengthy code in the main program by breaking it down into smaller, reusable parts.
Functions can have parameters, which allow you to pass variables into them. When declaring parameters, you can also specify default arguments.
Functions can return values using the return keyword, placed within the function’s block to send values back to the main program.
The primary purpose of a function is to avoid code duplication, enabling code reuse simply by calling the function’s name.
In Python, there are several key points to consider when declaring a function:
- Use the def keyword to declare a function.
- Use the return keyword for functions that return a value.
- Functions can have parameters/arguments.
- Python functions support parameters with initial values/default parameters.
Functions are generally 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 used to encapsulate a block of code solely to shorten the program’s length and are typically called independently within the program. Here’s an example:
1 | # Example of a Void Function (does not return a value) |
Functions that Return a Value
These functions return a specific value and are usually called and stored in a variable. Here’s an example:
1 | # Example of a Function that Returns a Value |
Block Program Inside a Function
After understanding functions, their categories, and usages, the next topic is the content within the function block. Before studying the contents of a function block, it’s helpful to grasp the underlying concepts. For instance, if you have a debit/credit card with a VISA/MASTERCARD logo, you can use it for transactions both nationally and internationally. However, if your card lacks these logos, it can only be used for domestic transactions. This illustrates the concept of access restriction and feature limitations, which applies similarly to function blocks. Once you grasp this concept, we will explore global and local variables.
Global Variables
Global variables can be used in any program block (inside function definitions or outside function declarations). Because they are globally accessible, global variables can be called and used anywhere, whether within the same file or across different files.
Local Variables
Local variables can only be used within a specific function/class block. Due to their limited scope, they can only be utilized within the function/class declaration block and cannot be accessed outside of it.
Examples of Functions:
Function with Parameters that Returns a Value:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19# Example of a function with parameters
g_int_result = 0 # Declaring a Global Variable
def fg_int_addFunctionWithParameters(value1, value2): # Function Declaration with Parameters
l_int_result = value1 + value2 # Declaring a Local Variable
# Outputting the local variable value in a function
# 1. First Method: Assign it to a global variable (For C/C++ programming)
# g_int_result = l_int_result
# print(g_int_result)
# 2. Second Method: Make the function return the local variable (Works in Python and C/C++)
return l_int_result
g_int_result = fg_int_addFunctionWithParameters(5, 6) # Calling the function and storing the returned value in the global variable
print(g_int_result)
Function with Default Parameter/Initial Value that Does Not Return a Value:1
2
3
4
5def subtractFunctionWithParameters(value1=5, value2=1): # Function Declaration with Default Parameters
result = value1 - value2
print(result)
subtractFunctionWithParameters() # If no values are provided, the function will automatically use the default parameters: value1 as 5 and value 2 as 1
Recursive Function
A recursive function is one that calls itself when declared. It is usually used as a substitute for loops, but it is rarely used because it consumes considerable computer resources, causing the computer to work harder compared to using traditional looping concepts.
Here’s an example of using a recursive function for calculating factorial:
1 | factorial(5) = 5 * factorial(4) |
1 | def fg_int_factorial(l_int_value): |
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.