π Python Functions: The Fundamental Building Block Of Any Program

In Python, functions are a fundamental building block of any program. They allow you to organize your code into logical, reusable units that perform specific tasks. Instead of repeating the same code several times, you can define it once inside a function and call it whenever needed.
πΉ What is a Function?
A function is a block of organized, reusable code designed to perform one specific job.
Functions help you:
- Avoid repetition in your code.
- Make your program modular, readable, and easier to maintain.
A function must be defined before it can be used.
πΉ Defining a Function
In Python, you define a function using the def keyword.
Hereβs the basic syntax:
def function_name():
# function body
statements
Example:
def hello():
print('Hello!')
Explanation:
defβ tells Python you are defining a function.helloβ the name of the function.()β parentheses that may contain parameters.:β marks the beginning of the function body.- The indented lines beneath are the function body (the actions to perform).
To execute a function, call it using its name followed by parentheses:
hello()
Output:
Hello!
πΉ Using Parameters
Functions often need input values to work with. These inputs are called parameters.
Example:
def hello(name):
print('Hello ' + name + '!')
When you call the function:
hello('Roger')
Output:
Hello Roger!
Key Terms:
- Parameter: A variable inside the parentheses of the function definition.
- Argument: The actual value supplied when calling the function.
Example:
def greet(name): # 'name' is a parameter
print(name)
greet('Zino') # 'Zino' is an argument
πΉ Default Parameter Values
Sometimes, you want a function to work even if no argument is passed. You can assign a default value to a parameter.
def hello(name='my friend'):
print('Hello ' + name + '!')
Examples:
hello()
# Output: Hello my friend!
hello('Syd')
# Output: Hello Syd!
If no argument is provided, Python uses the default value.
πΉ Multiple Parameters
You can define functions that accept more than one parameter.
def hello(name, age):
print('Hello ' + name + ', you are ' + str(age) + ' years old!')
Example call:
hello('Roger', 8)
Output:
Hello Roger, you are 8 years old!
πΉ Mutable vs Immutable Values in Functions
When you pass a value into a function, what happens inside the function can depend on whether the value is mutable or immutable.
Example with an immutable type (integer):
def change(value):
value = 2
val = 1
change(val)
print(val)
Output:
1
Explanation:
Integers (and other immutable types such as strings and tuples) cannot be changed in place. The variable val remains 1 even after the function runs.
Example with a mutable type (list):
def modify(data):
data.append(99)
numbers = [1, 2, 3]
modify(numbers)
print(numbers)
Output:
[1, 2, 3, 99]
Explanation: Lists are mutable, meaning changes inside the function affect the original list.
πΉ Returning Values
A function can send a result back to the part of the program that called it using the return statement.
Example:
def hello(name):
print('Hello ' + name + '!')
return name
When you call the function:
returned_name = hello('Zino')
print(returned_name)
Output:
Hello Zino!
Zino
If a function has no return statement, Python automatically returns None.
Conditional return example:
def greet(name):
if not name:
return
print('Hello ' + name + '!')
πΉ Returning Multiple Values
A Python function can return more than one value. When this happens, Python packs the values into a tuple.
Example:
def hello(name):
print('Hello ' + name + '!')
return name, 'Roger', 8
Calling:
print(hello('Syd'))
Output:
Hello Syd!
('Syd', 'Roger', 8)
The result ('Syd', 'Roger', 8) is a tuple containing multiple values.
πΉ Summary Table
| 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!
Complete each sentence by filling in the correct keyword or term.
- The keyword used to define a function in Python is
_________. - The values passed into a function when itβs called are called
_________. - A variable inside a function definition that receives data is called a
_________. - A function that doesnβt include a
returnstatement automatically returns_________. def greet(name='friend'):β here'friend'is a_________ value.- Functions help programmers avoid code
_________. return x, y, zreturns a single_________containing multiple values.- Lists and dictionaries are
_________, meaning they can be changed in place. - To execute a function, you must
_________it using its name and parentheses. - Numbers and strings are
_________, meaning they cannot be changed in place.