Skip to main content
Noble Desktop/3 min read

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.

Master Python at Noble Desktop

Noble Desktop's Python Programming Immersive covers AI APIs, data analysis, and modern Python development.

In this video, we're going to look at how to call a function using Python.

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. 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.

Let's create a function called "area". This function will take two arguments, length and width. Area will be length times width, and we want to return the area.

Now suppose we have a couple of rooms. Let's create Room 1 and call this function. This room is relatively small, so it's going to be 3 square feet by 5 square feet.

We'll also create Room 2 and it will be slightly bigger, 30 × 5. Lastly, let's create Room 3, which will be 5 × 7.

We can create a variable called "floor" and it will be Room 1 + Room 2 + Room 3. The total area of the floor would be 200 square feet.

Now, what about how do you call a function within some other function? Let me show you. Let's call this function "add" and it will take two arguments, A and B. This function will add two values together, A plus B.

Let's create some other function called "multiplication" and it will take two arguments, A and B. This time it will do A times B.

Now, suppose I have a function called "main" that will call other functions. This function will take two arguments, A and B.

Let's create a variable called "addition" and it will add A plus B. Now, we don't have to write this formula here, because we already have a function defined above that adds.

We'll also create another variable called "multiplication" and it will multiply A and B.

Now I want to return addition and multiplication. Make sure you run all the cells in the sequential order.

Now I can call this function "main" and pass 5 and 6. Our function main returns the results of two operations, addition and multiplication.

Since this function returns more than one result, these results are returned in the form of a tuple. If you don't know what a tuple is, watch my other video explaining data structures, an important topic in Python. You can also watch my video about lists, strings, and tuples. Thank you.

Video Transcript6 sections

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.