Once upon a time in Codeville, there lived a young wizard-in-training named Zino. He was curious, bright, and full of questions about the mysterious world of Python programming.

One fine morning, he met a wise old programmer named Mr. Byte, who said:

“Zino, if you want to master Python’s magic, you must first learn the secret art of functions.”


🧠 What is a Function?

Mr. Byte explained:

“A function is like a reusable spell — a block of organized code that performs a specific task when called.”

In simpler terms:

  • A function is a named section of code that can be executed whenever you need it.
  • It helps you avoid repetition and organize your code neatly.

✨ The Birth of a Function

Mr. Byte drew his wand (keyboard) and wrote:

def hello():
    print('Hello!')

He explained:

  • def → the keyword that defines a function.
  • hello → the name of the function (you can choose any valid identifier).
  • () → parentheses that can hold parameters (more on that soon).
  • : → indicates the start of the function body.
  • The indented line(s) → the body of the function — what it does.

To make this spell work, Zino had to call it:

hello()

Output:

Hello!

“Congratulations,” said Mr. Byte. “You’ve written your first function!”


🎯 Functions That Listen — Using Parameters

Zino wanted to greet people personally — not just “Hello!”, but “Hello Zino!” or “Hello Nia!”

Mr. Byte smiled and said, “Add a parameter to your function.”

def hello(name):
    print('Hello ' + name + '!')

Now, when Zino called:

hello('Roger')

Output:

Hello Roger!

🧩 Definition Time

  • Parameter: A variable inside the parentheses of a function definition — it receives data.
  • Argument: The actual value you pass when calling the function.

So:

def greet(name):   # 'name' is a parameter
    print(name)

greet('Zino')      # 'Zino' is an argument

😎 Default Parameters — When You’re Feeling Lazy

Zino didn’t always want to type a name. So, Mr. Byte revealed another trick — default values.

def hello(name='my friend'):
    print('Hello ' + name + '!')

Now:

hello()

Output:

Hello my friend!

But if he did pass a name:

hello('Syd')

Output:

Hello Syd!

📦 Multiple Parameters — More Gifts in One Box

Sometimes, you need more than one input. No problem!

def hello(name, age):
    print('Hello ' + name + ', you are ' + str(age) + ' years old!')

Calling:

hello('Roger', 8)

Output:

Hello Roger, you are 8 years old!

🎲 Changing Values Inside a Function

Zino asked, “If I change a value inside a function, does it change outside too?”

def change(value):
    value = 2

val = 1
change(val)
print(val)

Output:

1

Mr. Byte explained:

“Numbers and strings are immutable — they cannot be changed in place. But lists and dictionaries are mutable, so changes inside a function can affect the original.”

Example with a list:

def modify(data):
    data.append(99)

numbers = [1, 2, 3]
modify(numbers)
print(numbers)

Output:

[1, 2, 3, 99]

🎁 Returning Values — Functions that Give Back

A function can also return information to the caller.

def hello(name):
    print('Hello ' + name + '!')
    return name

Now:

returned_name = hello('Zino')
print(returned_name)

Output:

Hello Zino!
Zino

🔍 What is return?

  • The return statement sends a result back to where the function was called.
  • If you don’t use return, the function sends back None by default.

You can also conditionally return:

def greet(name):
    if not name:
        return
    print('Hello ' + name + '!')

🎉 Returning Multiple Treasures

Yes, functions can return more than one value!

def hello(name):
    print('Hello ' + name + '!')
    return name, 'Roger', 8

When Zino called:

print(hello('Syd'))

Output:

Hello Syd!
('Syd', 'Roger', 8)

This returns a tuple — a collection of values packed together.


🧙🏽‍♂️ Summary of Zino’s Function Spells

Concept Description Example
Function definition Creating a function using def def greet():
Function call Executing the function greet()
Parameter Variable in function definition def greet(name):
Argument Actual value passed in greet('Zino')
Default parameter Value used when no argument is given def greet(name='Friend'):
Return statement Sends data back to caller return x + y
Mutable object Can be changed (list, dict) [1,2,3]
Immutable object Cannot be changed (int, str, tuple) 5, 'hi'
Multiple returns Function returns several values return a, b, c

🔁 Quick Review: Fill-in-the-Gap Challenge!

Fill in the blanks with the correct word or symbol 👇

  1. The keyword used to define a function in Python is _______.
  2. The values passed into a function when it’s called are called _________.
  3. A variable inside a function definition that receives data is a _________.
  4. A function that doesn’t have a return statement automatically returns _________.
  5. def greet(name='friend'): — here, 'friend' is a _________ value.
  6. Functions help us avoid code _________.
  7. return x, y, z returns a single _________ containing multiple values.
  8. Changing a list inside a function affects the original list because lists are _________.
  9. To execute a function, you must _________ it by using its name followed by parentheses.
  10. A number or string cannot be modified in place because it is _________.

<
Previous Post
08-Binary Systems and Hexadecimal: Once Upon a Time in the Land of Numbers…
>
Next Post
10-Introduction to Programming With Python: Object-Oriented Programming