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 by 5. Lastly, let's create Room 3, which will be 5 by 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.