Skip to main content

✍️ Custom Functions

Level 1

Daily Practice

Step 1:
Write one function for each day of the week that simply prints the name of the day. Name your function the same as the day of the week while following our naming conventions (i.e. snake_case).

Step 2:
Write one more function called print_week() that calls all of the functions you wrote in Step 1, in the correct order.

Step 3:
Call (invoke) your function print_week() to print the names of the days of the week.

Daily Practice 2

Do something similar to the previous exercise, but do not print anything inside the 7 weekday functions. Only use a print() statement in the print_week() function. The output should be the same as the previous exercise.

If a function is not printing values, how can the code that called that function get something back for its own use?

Sumthings up

Write a simple function called calculate_sum() which takes two numbers as input and returns the sum. Call (invoke) the function calculate_sum() with values 3, 4 and print the returned value.

What would you expect this function to “return”

Summing squares

Consider the following function which implements this formula:

\[ n_1^2 + n_2^2 \]

def sum_squares(num1, num2):
    result = num1**2 + num2**2
    return result

What will be the output of this line of code:

print(sum_squares(3, 4) + sum_squares(1, 2))

The first call to sum_squares() passed in the values 3 & 4 so inside sum_squares it will do the math:

def sum_squares(num1, num2):
    result = 3**2 + 4**2
    return result # 25

The second call to sum_squares() passed in the values 1 & 2 so inside sum_squares it will do the math:

def sum_squares(num1, num2):
    result = 1**2 + 2**2
    return result # 5

After calling sum_squares twice, we effectively have this line of code:

print(25 + 5)
30

f of x

Write a function which helps apply this formula:

\[ f(x) = (x-4)(x+7) \]

Then, use it to evaluate \(f(x)\) for the values of \(x\):

  • \(x = 4\)
  • \(x=-7\)
  • \(x=2\)
  • \(x=-6\)

What would you expect this function to “return”

A matter of threes

Write a function which helps apply this formula:

\[ f(x,y) = 3(x+y) \]

Then, use it to evaluate \(f(x,y)\) for the following values:
- \(x = 3\) , \(y=4\) - \(x = 5\) , \(y=6\) - \(x = 2\) , \(y=8\)

Tracing

Consider the following functions:

def function1():
  print("Hi")
  function2()
  function3()

def function2():
  print("How are you?")

def function3():
  print("Bye")
  1. What will be the output when calling function1():
function1() 
Hi
How are you?
Bye
  1. What will be the output when running those lines of code:

    function1()  
    function2()
    function3()
Hi
How are you?
Bye
How are you?
Bye

Level 2

The Average

Consider the following function:

def calculate_average(num1, num2):
    result = (num1 + num2) / 2

Part 1: What will be the output of the following lines:

average = calculate_average(3, 4)

print(average)

None

Part 2: Explain the reason for this outcome and propose an improvement.

The function didn’t return the average. The calculation was done and the average was saved into a variable called result but nothing was returned. When you try to use the result of a function that didn’t actually return anything, Python gives you a special value called None which means nothing.

To fix this we should add a return statement:

Fun Circles

Write a simple function called area_circle() which takes the radius as parameter and returns the area:

\[ A_{circle} = \pi r^2 \]

  • Calculate the area of a circle with radius = 2 by using the function defined above.
  • Calculate the area of a circle with radius = 9 by using the function defined above.
  • Calculate the area of a ring which has an inner circle of radius 4 and an outer circle of radius 6.

What do you need to import for this to work?

More areas

  1. Write a simple function called area_rect() which takes the width and height of a rectangle and returns the area: \[ A_{rect} = w * h \]

  2. Write a simple function called area_square() which takes the side and returns the area: \[ A_{square}= s^2 \]

  3. Using the area_cricle(), area_rect() and area_square() functions, calculate the area of the following shape. You can assume that the red squares are perfect, that the green central ring is composed of two perfect inner and outer circles and that the blue borders are all perfect rectangles. :

Calculus ?!

Don’t run away if you haven’t taken Cal 1 yet! Read through this part and you’ll see that there’s just a pattern you need to follow…

Given some function: \[ f(x)=x^{n} \]

where n is the exponent of x. The derivative of this function is given by something called the power rule: \[ f'(x)=nx^{n - 1} \]

For example, if we have the function: \[ f(x)=x^{3} \] then the derivative is: \[ f'(x)=3x^{2} \]

Write a function called power_rule that has one parameter called exponent. The result of the function will be a string representation of the derivative with respect to x. Your string should look like: “12x**2” to represent \[12x^2\]

Try your function like:

derivative1 = power_rule(3)
print(derivative1)

Your function must return a string. An f-string would be really useful here!