Learning Rooms/△ Level 2 · Builder/Robotics and control
Python Tutorial for Beginners 8: Functions
Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play
By the end you can…
- Define a function with parameters and a return value, and call it with arguments.
- Explain the difference between returning a value and printing it.
- Write a docstring that states what the function does and the units of its inputs and output.
Watch it all: no segment has been checked by hand for this video yet.
1.Predict before you press play
def double(x): print(x * 2). What is stored in y after y = double(5)?
Pick one before you press play. A wrong guess is fine: it is what makes the video stick.
2.See the idea first
Play with this diagram for a minute. It gives you something to hang the video on.
Loading the interactive…
Can't use the diagram? Read a description instead
The function machine shows a Python function with a docstring, three versions of its body and a set of assert tests. For wheel_speed_m_per_s: version A (rpm / 60 * math.pi * diameter_m) passes all three tests; version B forgets to divide by 60 (60 rpm, 0.1 m gives 18.85 instead of 0.314 m/s); version C uses 2π with the diameter (0.628 m/s).
3.Watch, actively
Defining functions with parameters and return values, so one calculation can be reused and tested.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
Watch for…
- → Watch the difference between printing inside a function and returning a value, and how arguments fill the parameters.
Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.
Can't stream it? Read the idea instead
A function packages a calculation under a name. Parameters carry values in and return sends the result back, so the same code can run with different inputs and be tested on its own. A function that prints instead of returning cannot be reused in a bigger calculation.
What the video covers (our own note, written when we verified it): Defining functions with parameters and return values, so one calculation can be reused and tested.
Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.
Read it instead
- Lesson: Package a calculation as a function · The full lesson explains the idea in text, worked examples and diagrams, with no video needed.
- Engineering Atlas: Functions and decomposition · Writing a robot's wheel-speed conversion once as a function means a single fix reaches every part of the code that uses it.
- Field Guide: Functions and return values · A short reference entry on the same idea.
Jot down one thing that surprised you and one question you still have.
4.Practise what you just saw
These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.
Practice 1
def area_m2(width_m, height_m): return width_m * height_m. What does area_m2(3, 0.5) return?
Practice 2
def f(x): x * 2 (no return). What does f(4) give?
Practice 3
Using v = rpm / 60 × π × d, what speed in m/s does a 0.2 m wheel at 30 rpm give, to 3 decimal places?
Practice 4 · a fresh version every time
def wheel_speed_m_per_s(rpm, diameter_m): return rpm / 60 * math.pi * diameter_m. What does wheel_speed_m_per_s(150, 0.05) return, in m/s?
5.Try it yourself
No simulation models this idea yet. The lesson below has an interactive diagram instead.
6.Where engineers use it
Robot firmware has functions like this for every conversion (encoder ticks to metres, ADC counts to volts). A unit-conversion fix in one function reaches everywhere at once.
From the applied view in the lesson Package a calculation as a function. Checked by automated tests only.
7.Build something with it
Follow-up project
Write rpm_for_speed(speed_m_per_s, diameter_m), the inverse of wheel_speed_m_per_s, with a docstring. Check that wheel_speed_m_per_s(rpm_for_speed(0.5, 0.065), 0.065) gives 0.5 (within a tolerance).
Where this video lives
- Package a calculation as a function · Level 2, week 7
In the course catalogue: CSE-210 Python for engineers
The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.