Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ 戻り値のない関数 | 関数
Python入門

book戻り値のない関数

メニューを表示するにはスワイプしてください

return文のない関数は、メッセージの表示データの変更、またはプログラム内でのアクションの実行などのタスクを実行する再利用可能なセクションとしてコードを構造化したい場合に役立ちます。

注意

Pythonでは、すべての関数が値を返します。関数に明示的なreturn文が含まれていない場合、自動的にNoneが返されます。

アレックスが値を返さない関数の作成と使用方法をどのように示しているか見てみましょう。

return文のない関数の使い方を理解する最良の方法は、実際の例を見ることです。いくつかの例を見てみましょう。

コンソールへの情報表示

関数の目的が、特定のイベントや結果をユーザーに通知するためにコンソールへ情報を表示することだけの場合もあります。

例えば、total_sales()関数は計算を行い、その結果をすぐに表示することが主な役割なので、値を返す必要はありません。

1234567
# Prices of items sold today prices = [12.99, 23.50, 4.99, 8.75, 15.00] def total_sales(prices): print(f"Today's total sales: $", sum(prices)) total_sales(prices)
copy

注意

データや関数が宣言される順序は重要ではありません。唯一重要なルールは、関数は呼び出される前に定義されていなければならないということです。

データ構造の変更

開発者は、リストや辞書などのデータ構造を変更するが値を返さない関数を作成する必要がよくあります。

例えば、update_inventory() 関数は items_sold に基づいて在庫レベルを調整します。この関数は**inventory 辞書を直接変更する**ため、何も返す必要はありません。

12345678910111213141516171819202122232425
# Define the function that adjusts inventory levels def update_inventory(inventory, items_sold): # Iterate over each item in the dictionary for product, quantity_sold in items_sold.items(): # Decrease the inventory by the quantity sold for each product inventory[product] -= quantity_sold # Inventory dictionary inventory = { "apples": 50, "bananas": 75, "oranges": 100 } # Items sold dictionary items_sold = { "apples": 5, "oranges": 15 } # Update the inventory based on items sold update_inventory(inventory, items_sold) # Display the updated inventory print("Updated inventory:", inventory)
copy

別の関数の呼び出し

特定の条件を監視し、必要に応じて他の関数を呼び出す関数を作成することは一般的です。

例えば、check_stock_levels() 関数は商品の在庫レベルが設定したしきい値を下回っているかどうかを確認します。もし下回っていれば、restock() 関数を呼び出して追加の在庫を注文します。

この方法では値を返す必要はなく、主な目的は補充プロセスを開始することです。

12345678910111213141516171819202122232425
# Dictionary representing the current stock of products inventory = { "apples": 17, "bananas": 75, "oranges": 2, "grapes": 50 } # Function to restock items that have low stock levels by adding a specified amount def restock(product, inventory, restock_amount): inventory[product] += restock_amount print(f"Restock order placed for {product}. New stock level: {inventory[product]} units.") # Function to check which items are below the stock threshold and trigger the `restock` function def check_stock_levels(inventory, threshold): for product, quantity in inventory.items(): if quantity < threshold: # If the stock is below the threshold, call the `restock` function to add 50 units restock(product, inventory, 50) # Checking the stock levels for all products in the inventory with a threshold of 30 units check_stock_levels(inventory, 30) # Display the final inventory after restocking print("Final inventory status:", inventory)
copy
question mark

Python では、すべての関数が値を返します。関数に return 文がない場合、何が返されますか?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 6.  5

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 6.  5
some-alt