Functions — Python 練習ワークシート
A function is a named, reusable block of code. You define it once with def and call it by name as many times as you need. Functions keep programs organized and eliminate repeated code.
Parameters are the variable names listed in the definition — def greet(name): has one parameter, name. Arguments are the actual values passed in a call — greet('Ada') passes the argument 'Ada'. The argument is copied into the parameter when the function runs.
The return statement sends a value back to the caller and ends the function immediately. A function without a return statement gives back None. Printing inside a function is not the same as returning — print shows a value on screen, while return hands a value back for further computation.
Scope controls where variables are visible. A variable created inside a function is local to that function and disappears when it ends; a variable created outside is global to the script. Assigning to a variable inside a function creates a new local variable rather than changing the global one.
Tracing function calls means following the argument values into the parameters, executing the body, and substituting the returned value back at the call site — essential for predicting program output.
練習するスキル
- Defining functions with def
- Distinguishing parameters from arguments
- Using return values in expressions
- Tracing nested function calls
- Predicting local vs global variable behavior
- Knowing when a function returns None
練習ワークシート:Functions
指示: 各問題を丁寧に解きましょう。途中式もはっきりと書きましょう。最終的な答えは、指定された場所または指示に従って別の用紙に書いてください。
-
1.Write a Python function named is_even that takes one integer parameter n and returns True if n is even and False otherwise.
Functions — 練習ワークシート(続き)
-
2.What is the output of the following code? Show your reasoning. def mystery(x, y): result = x * 2 + y return result print(mystery(3, 5))
Functions — 練習ワークシート(続き)
-
3.Consider the following function definition: def greet(name, greeting="Hello"): return greeting + ", " + name + "!" What will be the output of each of the following function calls? a. greet("Alice") b. greet("Bob", "Hi") c. greet(greeting="Hey", name="Carol")
Functions — 練習ワークシート(続き)
-
4.Write a function named calculate_average that takes a list of numbers as a parameter and returns the average (mean) of those numbers. You may assume the list is not empty.
Functions — 練習ワークシート(続き)
-
5.What is the output of the following code? Explain what happens with the variable x. def modify(x): x = x + 10 return x x = 5 modify(x) print(x)
Functions — 練習ワークシート(続き)
-
6.Which of the following is the correct way to define a function in Python that takes two parameters and returns their sum?
- A. def add(a, b): return a + b
- B. function add(a, b) { return a + b; }
- C. def add(a, b): print(a + b)
- D. def add(a, b) return a + b
Functions — 練習ワークシート(続き)
-
7.Write a function named count_vowels that takes a string as a parameter and returns the number of vowels (a, e, i, o, u) in the string. Your function should work for both uppercase and lowercase letters.
Functions — 練習ワークシート(続き)
-
8.What is the output of the following code? def outer(x): def inner(y): return y * 2 return inner(x) + 1 print(outer(5))
Functions — 練習ワークシート(続き)
-
9.Write a function named find_max that takes a list of numbers as a parameter and returns the largest number in the list. Do not use the built-in max() function.
Functions — 練習ワークシート(続き)
-
10.Consider the following function: def process(data, multiplier=2, offset=0): result = [] for item in data: result.append(item * multiplier + offset) return result What will be the output of each of the following function calls? a. process([1, 2, 3]) b. process([1, 2, 3], 3) c. process([1, 2, 3], offset=5) d. process([1, 2, 3], 3, 1)
解答集
-
1.
最終回答: def is_even(n): return n
-
2.
The function mystery computes , which is then printed.最終回答: 11
-
3.
最終回答: a. "Hello, Alice!" b. "Hi, Bob!" c. "Hey, Carol!"
-
4.
最終回答: def calculate_average(numbers): total = 0 for num in numbers: total += num return total / len(numbers)
解答集(続き)
-
5.
The variable x inside the function is a local copy. Modifying it inside the function does not affect the global x, so when print(x) is called, it still prints the original value of 5.最終回答: 5
-
6.
最終回答: a
-
7.
最終回答: def count_vowels(s): vowels = "aeiouAEIOU" count = 0 for char in s: if char in vowels: count += 1 return count
-
8.
The inner function inner doubles its argument (), and then the outer function adds 1, giving .最終回答: 11
解答集(続き)
-
9.
最終回答: def find_max(numbers): largest = numbers[0] for num in numbers: if num > largest: largest = num return largest
-
10.
最終回答: a. [2, 4, 6] (multiplier=2, offset=0) b. [3, 6, 9] (multiplier=3, offset=0) c. [7, 9, 11] (multiplier=2, offset=5) d. [4, 7, 10] (multiplier=3, offset=1)
避けるべきよくある間違い
- Confusing print with return
- Calling a function before it is defined
- Expecting a local variable to exist outside its function
- Passing the wrong number of arguments
- Forgetting that a function without return gives None
自分だけのワークシートを作成
好きな難易度でfunctionsの問題を新しく作成したいですか?WorksheetSmithを使えば、印刷可能なカスタムPDFを数秒で作成できます。
最終更新:2026