Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON FOUNDATIONS / CHAPTER 01 / Learn / LESSON 07
Seventh class. If you copy a formula into three places, a bug means fixing it three times. Functions solve that.
def name(parameters): creates a recipe. return sends a result back to the call site. Calling calculate_bmi(weight, height) runs the recipe with concrete values.
Today you build one clear function: calculate_bmi. It receives weight and height and sends the result back with return. Calling code stores that result and prints it. This highlights an important distinction: return gives a value to the program; print only displays text to a person.
Mini example:
def double(n):
return n * 2
print(double(5))Common mistake: printing inside the function instead of returning, or forgetting to call the function.
Note: today we only learn functions (def / return). Python modules and import come in a later chapter.
Parameters receive data, return sends the result back, and print stays at the call site.
MODULES IDLE
›