このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Name a block of code
- A function is named code you can use again and again.
- Make one with
def name():and an indented body. - "Calling" the function by its name runs that code.
English
Give it inputs (parameters)
- The names in the brackets are parameters — the function's inputs.
- When you call it, you pass in arguments (the real values).
- A function can take more than one parameter.
English
def shout(word):
print(word.upper() + "!")
shout("hello")
English
Send back a result (return)
returnsends a value back to whoever called the function.- You can store that value or use it in an expression.
returnalso ends the function straight away.

English
def double(x):
return x * 2
result = double(5)
print(result)
English
return vs print
printonly shows something on the screen.returngives a value back that the rest of your code can use.- A function with no
returngives backNone.
English
In Cambridge pseudocode
- A
FUNCTIONreturns a value; aPROCEDUREjust does an action.
FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER
RETURN A + B
ENDFUNCTION
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
English
Common mistakes
- A function returns
Noneunless you writereturn; printing is not the same as returning. - Run a function with
():greet(), notgreet. - Define a function before you call it.
English
Now you try
- Define each function, then the checks will call it for you.
- Use
returnto send the answer back (do notprintit).
探索
Calling a function
A call pushes a frame with its arguments; return pops it.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Write a function add(a, b) that 回报(利回り) the sum of a かつ b.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Write a function greet(name) that 回报(利回り) the string Hello, then the name then !. So greet("Sam") 回报(利回り) Hello, Sam!.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Write a function biggest(a, b) that 回报(利回り) the larger of the two values. If they are equal, return either one.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Write is_palindrome(word) that 回报(利回り) True when the word reads the same backwards (like racecar), otherwise False.
実行ボタンをクリックして出力を確認してください。