Back to main page

Defining a function

In Python, functions are defined via the following syntax:


where you replace NAME with (almost) anything but a Python keyword. Ideally, however, the function should be named so it is obvious what the function is supposed to do. Conventionally, it is typical to name functions using snake_case. The parameters are, for the most part, treated as variables and named as such, and STATEMENTS represents the rest of the function.

Function with no parameters or return value

As a concrete example, consider the following function:

def print_hello_world():
    print('Hello, world')

To actually run this function, we call it by adding a line print_hello_world(). Thus, we might run something like

def print_hello_world():
    print('Hello, world')


Function with single parameter and no return value

Suppose we want to upgrade our print_hello_world so that it can print messages other than just a hello world. We might write something like the following:

def print_message(message):


Function with multiple parameters and a return value

Suppose we want to write a function that computes the rectangular form \(a + ib\) of the complex number \(re^{i\theta}\) assuming we know \(r \in \mathbb R\) and \(\theta\in\mathbb R\). By Euler’s formula, we know that \[ re^{i\theta} = r\cos\theta + ir\sin\theta. \] Thus, we might write the following:

import math

def polar_to_rect(r, theta):
    return r*math.cos(theta) + 1j*r*math.sin(theta)

print(polar_to_rect(2, math.pi/4))

This prints


This is, in fact, expected since \(2e^{i\pi/4} = \sqrt{2} + i\sqrt{2}\).

Scope of variables and parameters

Variables, including parameters, created inside a function only exist and can accessed within that function (without resorting to weird Python tricks, that is). So if we did something like this:

def print_this(a):
    print('You passed ' + str(a))


Python will complain about a syntax error:

Traceback (most recent call last):
  File "/home/itzsomebody/Desktop/", line 5, in <module>
NameError: name 'a' is not defined


  1. Consider periodic compounding. Let \(P\) be the original principal sum, \(r\) be the nominal annual interest rate, \(n\) be the compounding frequency, and \(t\) be the length of time we consider for interest. Then the total accumlated value \(A\) is given by the formula \[A = P\left(1 + \frac{r}{n}\right)^{nt}.\] Write a function, total_accumlated_value(p, r, n, t) that computes the total accumulated value. Test your function by printing the result for \(P = 100\), \(r = 0.05\), \(n = 12\), and \(t = 1\) (you should get 105.1161897881733 or similar).

  2. Recall that the formulas for converting Fahrenheit to Celsius is \[C = \frac{5}{9}(F - 32).\] Solving this formula for \(F\) gives us the formula for converting Celsius to Fahrenheit: \[F = 32 + \frac{9}{5}C.\] Write Python functions fahr_to_cels(temp) and cels_to_fahr(temp) that perform these conversions. Verify that your functions actually work by comparing to Google’s conversion calculator.

Back to main page