Functions — Fiche d'exercices de 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.
Compétences travaillées
- 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
Fiche d'exercices : Functions
Consignes : Résous chaque problème avec soin. Montre clairement tout ton travail. Écris ta réponse finale dans l'espace prévu ou sur une feuille séparée comme indiqué.
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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 — Fiche d'exercices (suite)
-
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)
Corrigé
-
1.
Réponse finale : def is_even(n): return n
-
2.
The function mystery computes , which is then printed.Réponse finale : 11
-
3.
Réponse finale : a. "Hello, Alice!" b. "Hi, Bob!" c. "Hey, Carol!"
-
4.
Réponse finale : def calculate_average(numbers): total = 0 for num in numbers: total += num return total / len(numbers)
Corrigé (suite)
-
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.Réponse finale : 5
-
6.
Réponse finale : a
-
7.
Réponse finale : 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 .Réponse finale : 11
Corrigé (suite)
-
9.
Réponse finale : def find_max(numbers): largest = numbers[0] for num in numbers: if num > largest: largest = num return largest
-
10.
Réponse finale : 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)
Erreurs fréquentes à éviter
- 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
Générez la vôtre
Vous voulez un nouvel ensemble de problèmes de functions à la difficulté de votre choix ? Utilisez WorksheetSmith pour compiler un PDF personnalisé prêt à imprimer en quelques secondes.
Dernière mise à jour : 2026