Functions
Python ↔ Baobab
10 correspondences
fonction replaces def, retourner replaces return. Default parameters, keyword arguments, *args/**kwargs and lambda work like in Python.
| Python | Baobab | Description |
|---|---|---|
| def f(a, b): … | fonction f(a, b): … | Definition with positional parameters. |
| return x | retourner x | Return a value (nul when omitted). |
| def f(x, n=2): … | fonction f(x, n=2): … | Parameter with a default value. |
| f(b=3, a=1) | f(b=3, a=1) | Call with keyword arguments. |
| def f(*args): … | fonction f(*valeurs): … | Variable number of positional arguments. |
| def f(**kwargs): … | fonction f(**nommés): … | Variable number of keyword arguments. |
| lambda x: x * 2 | lambda x: x * 2 | One-expression anonymous function. |
| return a, b | retourner a, b | Multiple return (implicit tuple). |
| g = f | g = f | Functions are first-class values. |
| """docstring""" | """docstring""" | Documentation embedded in the function. |
Complete example — Baobab ↔ Python
Baobab
123456789101112131415161718192021fonction puissance(base, exposant=2):
"""Renvoie base élevée à exposant (carré par défaut)."""
retourner base ** exposant
afficher(puissance(5)) # → 25
afficher(puissance(2, 10)) # → 1024
afficher(puissance(exposant=3, base=2)) # → 8
fonction moyenne(*notes):
retourner somme(notes) / longueur(notes)
afficher(moyenne(12, 15, 18)) # → 15.0
fonction min_max(valeurs):
retourner min(valeurs), max(valeurs)
bas, haut = min_max([4, 9, 1])
afficher(bas, haut) # → 1 9
doubler = lambda x: x * 2
afficher(doubler(21)) # → 42Python
123456789101112131415161718192021def puissance(base, exposant=2):
"""Return base raised to exposant (square by default)."""
return base ** exposant
print(puissance(5)) # → 25
print(puissance(2, 10)) # → 1024
print(puissance(exposant=3, base=2)) # → 8
def moyenne(*notes):
return sum(notes) / len(notes)
print(moyenne(12, 15, 18)) # → 15.0
def min_max(valeurs):
return min(valeurs), max(valeurs)
bas, haut = min_max([4, 9, 1])
print(bas, haut) # → 1 9
doubler = lambda x: x * 2
print(doubler(21)) # → 42