FINLANDIA SCHOOLFUNCTIONS
A function is a reusable block of code that performs a specific task. You "define" it once and can "call" (or "revoke") it many times
Why use functions?
a. DRY Principle (Don't Repeat Yourself): instead of writing the same 10 lines of code in five different places, you write it once inside a function and call it five times.
b. Abstraction: You can use a function without needing to know how it works, just what it does(e.g. you can use print() without knowing the complex code that sends text to a screen).
c. Organisation: Breaks a large, complex program into smaller, manageable, and understandable pieces.
Key Concepts of a Function
1. Definition: Creating the functions using a keyword (like def in Python or function in Javascript)
2. Calling: Uing the function's name to execute its code.
3. Paraameters (Arguments): Variables in the Function definition that act as inputs. When you call the function, you pass arguments to these parameters.
4. Return value: A Function can send a value back to the code that called it using the return keyword.
Code example( Python syntax)
1. DEFINITION of a simple function
def greet_student(student_name):
# student_name is a PARAMETER
print(f"Welcome to SS 3, {student_name}!")
print("Work hard this term.")
2. CALLING the function
greet_student("Tunde") #"Tunde" is the ARGUMENT
greet_student("Aisha")# We can reuse it easily
3. Funtion with a RETURN VALUE
def calculate_area(length, width):
#length and width are PARAMETERS
area=length*width
return area #SENDS this value back
4. CALLING a function with a return value
room_area=calculate_area(10,5) # the value 50 is returned
print(f"the area of the room is {room_area} square meters."
ARRAYS/LISTS
An Array(or list in Python, Array in Javascript) is a single variable that can hold a collection of of multiple items in an ordered sequence
Why use Array?
imagine needing to store the scores of 50 students. You could create 50 variables(score 1, score 2, score 3...score 50) which is a nightmare
or you could create one Array:scores=(72,85,...64)
Key concepts of an Array4
1. Element: The individual items inside the Array
2. Index: The position of an element. CRITICAL: The index almost always starts at 0, not 1. E.g.
In ["a","b", "c"], "a" is at index 0, "b" is at index 1, "c" is at index 2.
3. Accessing: You get an element using its index.
4. Iterating: You can use a for loop to "visit" every element in the array.
Code example(Python syntax):
1. CREATING a list
fruits ["Apple", "Banana", "Mango", "Orange"]
2. ACCESSING elements (index starts at 0)
first_fruit=fruits[0] # This will be "Apple"
third_fruit=fruits[2] # This will be "Mango"
3. MODIFYING elements
fruits[1]="Pawpaw" # Replaces "Banana"
# The list is now: ["Apple", "Pawpaw", "Mango", "Orange"]
4. ADDING elements
fruits.append("Guava")
# The list is now: ["Apple", "Pawpaw", "Mango", "Orange", "Guava"]
5. ITERATING (looping) through the list
print("My Fruit Basket:")
for fruits in fruits:
print(fruit)