Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ User-defined Functions | Functions & Modularity
Introduction to Python with Cursor

bookUser-defined Functions

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

In Python, a function is a named block of code for a specific task. You've already used built-ins like print() or len().

Defining your own functions helps avoid repetition, organize logic, and make programs clearer and easier to maintain.

Defining a Function

To define a function, you use the def keyword, followed by a name, parentheses, and a colon. The code that runs is written on the next line with indentation.

Parameters and Arguments

You can pass values into a function by listing parameters in the parentheses. These values are used inside the function when it runs.

Default Parameters

You can also give parameters default values. That means the function still works even if no argument is passed in.

Returning Values

Functions can give back a result using the return keyword. The returned value can be stored in a variable or used directly in expressions.

Scope and Variables

Variables that are created inside a function exist only within that function. They're not visible or accessible from the outside.

Summary

  • Use def to define a function;
  • Use parameters to pass input;
  • Use return to give a result back;
  • Variables inside a function are local;
  • Functions help structure and reuse code.
question mark

What keyword is used to define a function in Python?

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

すべて明確でしたか?

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

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

セクション 4.  2

AIに質問する

expand

AIに質問する

ChatGPT

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

セクション 4.  2
some-alt