Introduction to Python Functions: Basics of Definition with def, Arguments, and Return Values

In programming, a “function” is a way to group a specific process, give it a name, and make it reusable. Python has many built-in functions like print() and len(), but you can also create (define) your own custom functions.

This article explains how to define functions using the def statement, how to pass arguments, and how to receive return values.


目次

Defining and Calling Functions

To define a function, use the def keyword. You define the function name, arguments (variables that receive input), and the processing block (written with indentation).

Basic Syntax:

def function_name(arg1, arg2):
    # Process to execute
    return return_value

Specific Example: Calculating Tax-Included Price

As an example, let’s create a function that receives a product’s base price and tax rate, calculates the tax-included price, and returns it.

def calculate_tax_included_price(price, tax_rate):
    """
    Function to calculate tax-included price from base price and tax rate
    """
    tax_amount = price * tax_rate
    total_price = int(price + tax_amount)
    
    return total_price

# --- Calling the function ---

# 1000 yen product, 10% tax (0.1)
result1 = calculate_tax_included_price(1000, 0.1)
print(f"Calculation Result 1: {result1} yen")

# 5000 yen product, 8% tax (0.08)
result2 = calculate_tax_included_price(5000, 0.08)
print(f"Calculation Result 2: {result2} yen")

Output:

Calculation Result 1: 1100 yen
Calculation Result 2: 5400 yen

By defining the calculate_tax_included_price function, you can easily call the same calculation logic repeatedly even if the price or tax rate changes.


Return Values (return)

When a return statement is executed inside a function, the function’s processing ends at that point, and the value specified after return is sent back to the “caller.” In the example above, the calculated total_price becomes the return value and is assigned to variables result1 and result2.

If there is no return statement, or if you write only return without a value, the function returns a special value called None.


Functions without Arguments or Return Values

Not all functions return a calculation result. Some functions simply display a message on the screen or record a log.

Specific Example: System Startup Message

def show_system_status():
    """
    Display the current system status (No return value)
    """
    print("--- System Status ---")
    print("OS: Linux")
    print("Status: OK")
    print("---------------------")

# Calling the function (No arguments needed)
show_system_status()

# Try assigning the result of a function with no return value to a variable
status_result = show_system_status()
print(f"Content of return value: {status_result}")

Output:

--- System Status ---
OS: Linux
Status: OK
---------------------
--- System Status ---
OS: Linux
Status: OK
---------------------
Content of return value: None

In this example, the show_system_status function has no return statement. Therefore, when the function is called, the output via print is performed, but None is stored in the variable status_result.


Summary

  • Function Definition: Write in the format def function_name(arguments):.
  • Arguments: Data passed to the function. You can specify multiple arguments.
  • Return Value: Use the return statement to return a value from the function.
  • Reusability: By turning processes into functions, you avoid writing the same code multiple times, improving program readability and maintainability.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次