Just For Fun 05: Function Calls in Code
Understanding Functions, Flow, and Mathematical Reasoning
By the end of this lesson, you will be able to:
Introduction
In this lesson, you’ll explore the structure of Python code, focusing on how functions are defined and called. We’ll use a simple example that calculates the area of a square to illustrate key concepts.
Key Concepts
💡 Concept 1: Function Definition & Calls
A function in Python is a reusable block of code that performs a specific task. Functions are defined using the def
keyword, followed by the function name and parameters. To use a function, you “call” it by its name and provide the required arguments.
Example:
def squareArea(s: float) -> float:
""" determine area of square"""
return s * s
💡 Concept 2: Flow of Execution
Python executes code from top to bottom. The main()
function is often used as a “driver” to organize the flow. When main()
is called, it runs the code inside it, including any function calls.
Example:
def main() -> None:
= 5
sideLength print(f"Length {sideLength}")
print(f" Area: {squareArea(sideLength)}")
Example 1a: Calculating the Area of a Rectangle
What this code does: Defines a function to calculate the area of a rectangle and calls it with sample values.
Example Code:
def rectangleArea(length: float, width: float) -> float:
""" determine area of rectangle"""
return length * width
print(rectangleArea(6, 3))
Example 1b: Calculating the Area of a Circle
What this code does: Defines a function to calculate the area of a circle and calls it with a sample value.
Example Code:
import math
def circleArea(radius: float) -> float:
""" determine area of circle"""
return math.pi * radius * radius
print(circleArea(2))
Example 1c: Calculating the Volume of a Cube
What this code does: Defines a function to calculate the volume of a cube and calls it with a sample value.
Example Code:
def cubeVolume(s: float) -> float:
""" determine volume of cube"""
return s ** 3
print(cubeVolume(3))
Challenge Questions
- Choose a function to check whether a side can have a negative distance value. What should the function do if it receives a negative value?
- Change the one of the
circleArea
orcubeVolume
functions to handle a radius of zero or negative values. Should the area be zero or should it raise an error? - Challenge: Write a function to calculate the volume of a cylinder (
π * r^2 * h
). Try different values for radius and height. - Challenge: Write a function to calculate the area of a triangle given its base and height. Test your function with different values.
Use the interactive code fields above to modify and run your solutions. Have fun experimenting and solving these challenges!
Your Turn!
Challenge Tasks:
- Try using different types of inputs (integers, floats, strings, booleans, complex numbers) and observe the results.
- Modify the function to handle invalid inputs gracefully (e.g., using type checking or error handling).
- Add comments to explain each part of the code.
- Write your own function to calculate the area of a rectangle.
Use any of the terminals above to experiment with these challenges!
Summary
In this lesson, you explored: - The structure and anatomy of Python functions - How function calls work and how data flows through a program - Mathematical reasoning for calculating area - How different input types affect function behavior
Key Takeaways
- Functions are reusable blocks of code
- Flow of execution is organized using functions like
main()
- Mathematical formulas can be implemented directly in code
- Input types matter for function behavior