Write reusable functions
Define functions, pass arguments and return results.
- Create a function that accepts input and returns a value.
A function packages reusable work. Define one with def, give it a name and parameters, then indent its body. return sends a result back to the caller; print only displays a value.
1def greet(name):
2 return "Hello, " + name
3
4print(greet("Sam"))Hello, Sam
Parameters are names listed in the definition; arguments are values supplied when calling the function. A function without an explicit return gives back None.
Key takeaways
Create a function that accepts input and returns a value.
Read the output to check what your program did.
Keep indentation consistent in Python blocks.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Create a greeting function
Define greet(name) so it returns Hello, followed by the supplied name. Read one name and print the function result.
- Greet Ada
- Greet Lin
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…