Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Add the Area Function

00:00 With the shout() function ready, move on to calc.py.

00:05 Again, you can add the tasks as a comment and then format the comment so it looks a little bit like a small task list. In the calc module, add a function called area() that takes two parameters called length and width

00:23 and returns their product, length times width.

00:29 You already have the calc.py module, so the next step is to add a function. And this time, let’s add the parameters right away. So it should take two parameters, length and width. def area, parenthesis, and then the first parameter is length, and the next parameter is width, closing parenthesis, colon.

00:53 And then in the function body, you return something. And you know what? Let’s do it in one go this time. So it should return the product, length multiplied by width.

01:09 I am a little bit lazy, so I just copy and paste that part here and paste it after the return keyword. So now it says def area(length, width): And in the function body, return length * width.

01:26 So the asterisk is the symbol for multiplication.

01:30 To check if everything works, save the file and run the file in the interactive window and call it with area(). And now you can already see this tool pop up saying length and width need to be added.

01:46 I keep it simple for now and just add, like, a two and a three in it. So two times three should be six. Let’s see. Perfect. It works. So with that, you have tackled the second task of the challenge.

02:01 So in the calc module, you created a function named area() that takes length and width and returns their product.

02:10 Before you leave the file, clean up after yourself, so now you just have the function in it. And don’t forget to save the file. And now with this in place, we can tackle the next task of this challenge.

Become a Member to join the conversation.