Adela functionates

Tags

Hey, Adela! Can you write me a function? Called circle_circumference. You send it the radius of a circle, and it returns its circumference.

Adela
Adela

OK. So, the name is circle_circumference, it takes one parameter. Here's the first bit.

  • def circle_circumference(radius):
Adela
Adela

Hmm... I'll add the return, to remind myself the function sends something back.

  • def circle_circumference(radius):
  •     # Something here
  •     return circumference

Note

The name, parameters, and return type of a function are called the function's signature.

Adela
Adela

Now the missing bit.

  • def circle_circumference(radius):
  •     circumference = 2 * radius * 3.14159
  •     return circumference

Testing

Reflect

How would you test the code?

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Adela
Adela

I'd work out some circumferences I know are correct, and compare them with what my function computed.

Let's see... well, our googly friend can help.

Google results

Adela
Adela

I'll add some test code...

  • def circle_circumference(radius):
  •     circumference = 2 * radius * 3.14159
  •     return circumference
  •  
  • print(circle_circumference(5))
Reflect

What should the program print?

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Adela
Adela

It works, buckaroos! The program says 31.4159.

Ray
Ray

It's a little off, though, isn't it? Your code said 31.4159, but Google said 31.41593.

That's a rounding error, and is normal with floats.

If we want the program to be more accurate, we could use a more accurate value for π (pi). Python has one in the math library.

  • import math
  •  
  • def circle_circumference(radius):
  •     circumference = 2 * radius * math.pi
  •     return circumference
  •  
  • print(circle_circumference(5))
Reflect

What's the output now?

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Georgina
Georgina

I got 31.41592653589793. That's a tiny difference, compared to 31.4159. About a ten thousandth of a %. Ish.

Aye. If we were planning to send goats to Mars, it might be important. For most purposes, it doesn't matter.

Reflect

Type in a function called rect_area. It computes the area of a rectangle, given two parameters: width and length.

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site.
Ethan
Ethan

Here's what I got.

  • def rect_area(width, length):
  •     area = width * length
  •     return area
  •  
  • area = rect_area(5, 6)
  • print('Area:' + str(area))

Good!

Let's practice some more.