Stage 1 · Lesson 4 of 9
Functions, parameters and scope
A packages a piece of logic under a name. It takes inputs as parameters and hands a result back with return. Printing inside a function is not the same as returning.
A function without return gives back , which is why total = add(2, 3) can end up empty. Variables created inside a function are : they exist only there and disappear when it returns. Where a name can be seen is called its scope.
Reading a global variable from inside a function works. Assigning to one, though, creates a new local unless you write global, which you should rarely need. Learn default arguments (a value used when the caller leaves one out) and (passed by name).
Then learn the classic trap of , such as def add(item, items=[]): that one list is shared between every call. Small functions that return values are what make testing possible later.
A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking Python Fundamentals
Design Gurus · Beginner · $50
Beginner Python with an in-browser exercise after nearly every lesson
- CourseFreeCS50's Introduction to Programming with Python
Harvard University · Beginner
Free Harvard course whose weekly problem sets make you write real programs