Introduction:
In programming, we often need to break down complex tasks into smaller, manageable parts. These smaller parts are called subroutines. Subroutines are reusable blocks of code designed to perform specific tasks, and they come in two forms:
- Procedures: Subroutines that do not return a value.
- Functions: Subroutines that do return a value.
In many programming languages, procedures and functions are distinct concepts, but in Python, we don’t have separate keywords for them. Instead, everything is referred to as a function. However, depending on how we define it, a Python function can behave either like a procedure or a function:
- A procedure is simply a function that performs an action but doesn’t return any data.
- A function returns a result, which can be stored or used elsewhere in the program.
So in Python, every subroutine is technically called a "function," but it could behave like a procedure if it doesn’t return a value. This distinction is important when you want to understand the difference between functions that produce output for further use and those that just perform an action.
Real-World Example:
Imagine you are programming a game where you want to greet the player at the start:
- A procedure might print the greeting directly.
- A function might generate the greeting and return it, allowing the program to store or modify it before displaying.
Let's take a look at how a procedure and a function are set up in Python.
Defining a Function in Python:
To define a function in Python, we use the def
keyword followed by the function name and parentheses ()
which may include parameters. After the parentheses, a colon :
is used, and the function's body is indented beneath it.
Here’s the basic structure:
def function_name(parameters): # Function body (indented) # Optionally returning a value return result
For example, the following defines a function called greet_player()
that takes no parameters and performs a task without returning a value:
def greet_player(): print("Welcome to the game!")
And this function called get_greeting()
returns a value that can be used elsewhere:
def get_greeting(): return "Welcome to the game!"
Procedure Example (no return value):
Function Example (returns a value):
In the function example, the returned value can be stored in a variable and used later in the program.
Now that you understand the subtle difference between procedures and functions, let’s see how we can define both in Python.
Predict:
Look at the following code. Can you predict what the output will be?
def greet(): print("Hello!") greet()
In this example, the greet()
function behaves like a procedure because it doesn’t return anything—it only performs an action. What do you think will be printed?
Run:
Let’s try running a procedure and a function to see the difference in action. The procedure prints a message, and the function returns the result of multiplying two numbers.
Notice how the procedure simply prints the message, while the function returns a value that is stored and then printed.
Investigate:
Let’s investigate what happens if you mistakenly use a return
in what was meant to be a procedure. Try running the following code and observe the behaviour.
Does this behave like a procedure or a function? What is the effect of the return
statement here?
Modify:
Now let’s modify the following procedure into a function. It currently prints a message, but we want it to return the message instead. Modify the code to make this change.
Hint: Replace the print()
with a return
to convert this into a function.