0Pricing
Python For Kids · レッスン

引数と戻り値

関数にデータを渡し、結果を受け取る方法を学びます。

「引数と戻り値」はCoddyKit上の無料Python For Kidsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはPython For Kids学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Python For Kidsコースには全4レッスンが含まれています。

関数にデータを渡す

引数と戻り値

関数のレッスンへようこそ!今日は、引数を使って関数にデータを渡し、戻り値を使って結果を受け取る方法を学びます。

引数と戻り値 — イラスト1

関数の引数とは

引数は、関数が処理に必要なデータを渡すための値です。関数の柔軟性と再利用性を高めます。

  • パラメータ:関数の丸かっこの中に記述する変数です。
  • 引数:関数を呼び出すときに渡す値です。

Pythonの関数で引数がどのように機能するか見てみましょう。

関数で引数を使う

関数を定義するとき、関数の呼び出し時に渡す引数の受け皿となるパラメータを指定できます。

例:

# Defines a function that greets a user by name
def greet(name):
    greeting = "Hello, " + name + "!"  # Creates a greeting message
    print(greeting)  # Prints the greeting message

# Calls the function with the argument "Alice"
greet("Alice")  # Output: Hello, Alice!

デフォルト引数

関数のパラメータにデフォルト値を設定できます。関数を呼び出すときにそのパラメータへ引数が渡されなかった場合は、デフォルト値が使われます。

例:

# Defines a function with a default parameter
def greet(name="Python Learner"):
    greeting = "Hello, " + name + "!"  # Creates a greeting message
    print(greeting)  # Prints the greeting message

# Calls the function without an argument
greet()  # Output: Hello, Python Learner!

# Calls the function with an argument
greet("Bob")  # Output: Hello, Bob!

キーワード引数

関数を呼び出すとき、パラメータ名を使って引数を指定できるため、任意の順序で渡せます。

例:

# Defines a function that describes a person
def describe_person(name, age):
    description = name + " is " + str(age) + " years old."  # Creates a description
    print(description)  # Prints the description

# Calls the function using keyword arguments
describe_person(age=25, name="Charlie")  # Output: Charlie is 25 years old.

可変長引数(*args)

関数にいくつの引数が渡されるか、あらかじめ分からない場合があります。*argsを使うと、任意の数の位置引数を処理できます。

例:

# Defines a function that sums any number of numbers
def sum_numbers(*args):
    total = 0  # Initializes the total
    for number in args:
        total += number  # Adds each number to the total
    return total  # Returns the sum

# Calls the function with different numbers of arguments
print(sum_numbers(1, 2, 3))       # Output: 6
print(sum_numbers(4, 5))          # Output: 9
print(sum_numbers(10, 20, 30, 40))  # Output: 100

可変長キーワード引数(**kwargs)

*argsと同様に、**kwargsを使うと任意の数のキーワード引数(名前付き引数)を処理できます。

例:

# Defines a function that prints keyword arguments
def print_info(**kwargs):
    for key, value in kwargs.items():
        print(key + ": " + str(value))  # Prints each key-value pair

# Calls the function with various keyword arguments
print_info(name="Diana", age=28, city="New York")
# Output:
# name: Diana
# age: 28
# city: New York

戻り値

return文は、関数から呼び出し元へ値を返します。これにより、関数の結果をコードの別の部分で利用できます。

例:

# Defines a function that returns the square of a number
def square(number):
    result = number ** 2  # Calculates the square
    return result  # Returns the square

# Calls the function and stores the returned value
squared_value = square(5)
print("Square:", squared_value)  # Output: Square: 25

# Function that multiplies two numbers and returns the result
def multiply(a, b):
    product = a * b  # Calculates the product
    return product  # Returns the product

# Calls the function and stores the returned value
result = multiply(4, 5)
print("Product:", result)

よくできました!

Pythonで関数に引数を渡し、戻り値を使って結果を取得する方法を学びました。これらの概念により、関数の汎用性と機能性が高まり、動的なデータを扱ったり複雑な処理を実行したりできるようになります。さまざまな種類の引数や戻り値を使って自分の関数を作成し、練習を続けてください。

引数と戻り値 — イラスト10

よくある質問

「引数と戻り値」レッスンは無料ですか?

はい。「引数と戻り値」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Python For Kidsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Python For Kidsコースには全4レッスンが含まれています。

「引数と戻り値」で何を学びますか?

関数にデータを渡し、結果を受け取る方法を学びます。 ブラウザで直接実行するハンズオンコードでPython For Kidsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Python For Kidsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのPython For Kidsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「引数と戻り値」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このPython For Kidsレッスンでコードを書いて実行できますか?

はい。すべてのPython For Kidsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 関数とは?
  2. 自分で関数を定義する
  3. 引数と戻り値
  4. ラムダ関数
← Python For Kidsに戻る