Python 3.6 : Rescue buoy : parameters

Cette vidéo revient sur les paramètres de fonction en Python, point de confusion fréquent chez les débutants. La meilleure analogie : une fonction est une boîte noire. Vous l'appelez et récupérez un résultat, mais vous n'avez pas besoin de savoir ce qui se passe à l'intérieur. Les paramètres sont les "entrées" qu'on lui donne à chaque appel.

Fonctions avec et sans paramètres

Une fonction peut avoir zéro, un ou plusieurs paramètres. Quand elle en a, il faut obligatoirement fournir une valeur à l'appel, sinon Python lève une erreur "missing required argument" :

def demander_age(nom):
    return input(f"Quel est l'âge de {nom} ? ")

# Appel correct avec un argument
age = demander_age("Alice")

# Appel incorrect, manque l'argument
# age = demander_age()  # TypeError

def demander_nom():
    return input("Quel est ton nom ? ")

# Appel correct sans argument
nom = demander_nom()

# Appel incorrect, argument en trop
# nom = demander_nom("Bob")  # TypeError

Le programme principal et les fonctions sont deux mondes séparés. Une variable définie dans une fonction est locale à cette fonction : elle n'existe qu'à l'intérieur et disparaît dès que la fonction se termine. Le seul lien entre les deux mondes est le passage de paramètres à l'appel et la valeur retournée par la fonction (return).

Points clés à retenir sur les paramètres :

  • Une fonction sans paramètres : appelée avec parenthèses vides
  • Une fonction avec paramètres : un argument par paramètre déclaré, dans l'ordre
  • Mauvais nombre d'arguments = erreur TypeError au runtime
  • Les paramètres sont des variables locales, invisibles à l'extérieur
  • Le programme principal communique avec la fonction via les paramètres (entrée) et le return (sortie)

Cette séparation stricte entre intérieur et extérieur de la fonction est ce qui rend le code modulaire et testable. Chaque fonction est une unité indépendante. La prochaine vidéo proposera un exercice pour pratiquer la définition et l'appel de fonctions avec paramètres.

Summary

This lesson covers function parameters in Python, explaining how parameters work as local variables within functions and the critical rules for calling functions correctly. You'll learn the difference between functions with and without parameters, how to pass arguments when calling functions, and what errors occur when parameter requirements are not met.

Key points

  • Parameters are local variables that exist only within a function scope and cannot be accessed outside the function
  • Functions can be called with parameters (requiring arguments) or without parameters (no arguments needed)
  • When a function requires a parameter, you must always provide a matching argument during the function call, or a 'missing required positional argument' error will occur
  • If a function does not expect parameters, passing arguments will trigger an error
  • Parameters act as a communication mechanism between the main program and the function—think of functions as black boxes that receive input through parameters

FAQ

What is a parameter in Python, and how does it differ from an argument?

A parameter is a variable defined in the function definition that receives a value. An argument is the actual value you pass when calling the function. For example, in `def greet(name):`, 'name' is the parameter; when you call `greet("John")`, 'John' is the argument.

What happens if I call a function that requires a parameter but don't provide an argument?

Python raises a TypeError with the message 'missing one required positional argument', indicating that you forgot to pass a required value to the function.

Can a function have multiple parameters?

Yes, functions can have multiple parameters. Each parameter acts as a local variable, and you must provide a corresponding argument for each parameter when calling the function.