
Table of Contents(目次)
1. Understanding the Basic Structure of a Function
So far, you have learned about variables, data types, data structures, conditional statements, and loops. With these, you can already write simple programs.
However, as your programs grow longer, you might start feeling, “It’s annoying to write the same process repeatedly,” or “I want to organize similar processes together.” This is where functions come in.
A function is a mechanism that allows you to define a specific process as a single block. By using functions, you can call the same process repeatedly, making your program much more organized and readable.
Let’s first take a look at the minimal structure of a function.
def sample():
return
sample() # Execute the function
Memorize this form as the basic structure. Technically, you can omit the return statement without causing an error, but since a function “returns a result and then ends,” it’s best to learn it in this complete form from the start.
To define a function, the following elements are required:
def | def is an abbreviation for define, and this keyword is used to define functions. |
Function name | Write the function name after one space following def. The naming rules are the same as those for variables: use only alphanumeric characters and underscores, and do not start with a number. |
Parentheses () | Write parentheses after the function name. If the function needs parameters (values received from arguments), define them inside the parentheses. Even if no parameters are required, you must still include the parentheses. |
Colon : | The colon marks the end of the function definition line. |
return | This keyword ends the function’s process and optionally returns a value. |
To execute a function, write it like sample(). So far, you have executed functions like print() and round(); user-defined functions follow the same rule. If you omit the parentheses, Python will treat it as a reference to the function object rather than executing it. By including the parentheses, Python recognizes that it should execute the function.
2. Understanding the Difference Between “With return” and “Without return”
Using return allows you to terminate a function’s execution immediately. Conversely, if return is not present, the function automatically ends after executing the final line.
If a function doesn’t include a return statement, or if no return statement is reached during execution, the function ends after the last line and implicitly returns None.
Let’s compare a function with return and one without it:
def with_return():
print('Starting process: with_return()')
return
print('This line will not be executed')
def without_return():
print('Starting process: without_return()')
print('This line will also be executed')
with_return()
without_return()
When this code is executed, the output will be:
Starting process: with_return()
Starting process: without_return()
This line will also be executed
As you can see, in with_return(), the function terminates as soon as it reaches the return line, so the following line is never executed. In contrast, without_return() continues executing until the final line.
Think of return as a “switch that ends a function.” It’s especially useful when you want to terminate a process based on certain conditions.
3. Returning Values from a Function
So far, we’ve looked at how return ends a function, but it has another important purpose: returning a value. The value returned by return is called the return value.
If you don’t specify a return value, the function returns None. To return something else, write the desired value after a space following return.
By returning a value from a function, you can pass the “result of the process” back to the calling code.
Here’s an example:
def say_hello():
return 'Hello'
message = say_hello()
print(message)
The output will be:
Hello
When the say_hello() function is called, the string specified in the return statement is returned to the caller and assigned to the variable message.
If you want to return multiple values, separate them with commas, or combine them into a data structure such as a list or dictionary.
Here’s an example:
def sample():
a = 1
b = 2
c = 3
return a, b, c
print(sample())
Output:
(1, 2, 3)
When a function is defined to return multiple values, the returned values are grouped into a tuple.
4. Functions with Multiple return Statements
A function can include multiple return statements. This is useful when you want to return different values depending on certain conditions.
Let’s create a function that checks the current time and returns a greeting based on the time of day. You can get the current time using the datetime module.
import datetime
def greeting_now():
# Get the current hour
now = datetime.datetime.now().hour
if now < 12:
return 'Good morning'
elif now < 18:
return 'Good afternoon'
else:
return 'Good evening'
return 'This return statement will not be executed'
print(greeting_now())
When executed, the function will display an appropriate greeting message depending on the current time of day.
Here’s the key point to remember: while you can include multiple return statements in a function, the function ends as soon as the first one is executed. In other words, only the first matching return runs, and any code after it is ignored.
In the example above, since one of the if–elif–else conditions always triggers a return, the return on line 14 will never be executed.






