Calling a Function with Python
Function Call Patterns
Positional Args
func(a, b, c) — order matters.
Keyword Args
func(a=1, b=2, c=3) — order doesn't matter, more readable.
Default Values
def func(a, b=10): allows skipping b on call.
Unpack Arguments
func(*list) and func(**dict) unpack iterables and mappings.
Noble Desktop's Python Programming Immersive covers AI APIs, data analysis, and modern Python development.
1Full Video Transcript
Hi, my name is Zaret and I teach Python. In this video, I'm going to show you how to call a function within a function. I hope you watched my previous video and you know how to compose functions.
Suppose we have a floor full of rooms and we need to calculate the area of the floor. How do you calculate the area of a room? It's pretty simple—it's width times length. Let's create a function. Again, we start the function with the keyword def. I'm going to call this function area, and this function will take two arguments: length and width.
2Creating the Area Function
So we take arguments, and then what we want to do is calculate the area, which will be length times width. We want to return the result because, remember, functions take arguments and return results. We will return area. That's our function.
Now suppose we have a couple of rooms. Let's create room_one and we'll call this function. This room is relatively small, so it's going to be three square feet by five square feet. Then there's going to be room_two, which will be a slightly bigger room—30 by 5. And let's do one more: room_three would be 5 by 7. So we've got three rooms.