Calling a Function with Python

Step-by-step Python programming tutorial

Get an in-depth understanding of how to call a function within a function using Python. Taught by a Python instructor from Noble Desktop, this video tutorial is instrumental for those keen on enhancing their Python skills or building a career in Python programming.

Key Insights

  • The video provides a tutorial on how to create a function in Python to calculate the area of a floor with multiple rooms.
  • Three rooms with different dimensions are generated for the calculation demonstration.
  • The tutorial also explains how to create a function within another function, which can perform addition and multiplication operations.
  • The main function is designed to call the addition and multiplication functions and return the results in a tuple format.
  • The instructor underscores the importance of running all cells in sequential order when working with Python.
  • The video encourages learners to refer to other tutorials on data structures, lists, strings, and tuples for a comprehensive understanding of 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.

Play video

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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 Transcription:

Full 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.

Creating 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.

Calculating Total Floor Area

Now we could create a variable named floor, and it's going to be room_one plus room_two plus room_three. Now the total area of the floor would be 200 square feet. This is an example of how you could call a function and assign each result to a variable name so you could use them later.

But what about how do you call a function within some other function? Let me show you. I'm going to change this function. Let's call this add, and here I'm going to take two arguments, A and B. This is going to be a relatively simple function—it will add two values together: A plus B. Suppose we're building a calculator.

Building Multiple Functions

Then I'm going to create another function. I'm going to call this mult, and it also will take two arguments, A and B. But this time it will do A times B.

Now suppose I have another function. Let's call it main—that's going to be a main function that will call other functions. This function will also take two arguments, A and B. Here, let's create a variable named addition, and addition will add A plus B. But we don't have to write this formula here. Why? Because we already have a function defined above that adds. Remember, Python runs in sequence, so the add function is supposed to be on top.

Calling Functions Within Functions

So addition will call add with A and B. Then there's going to be another variable, multiplication, and this will call the function mult that's defined above with A and B. Now I want to return both addition and multiplication.

Make sure you run all the cells in sequential order. Now I could call this function main and pass in 5 and 6. You see our function main returns the results of two operations: addition and multiplication.

Understanding Return Values and Tuples

By the way, since we're on the subject, let me remind you that when a function returns more than one result, these results would be returned in the form of a tuple. I hope you know what a tuple is. If you don't, watch my other video explaining data structures—an important topic in Python. You could also watch my video about lists, strings, and tuples. Thank you.

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram