5 Algorithmic Thinking

5.3 Defining Functions

Goals:
Learn how to create a function with def;
Learn how to return results;
Create a new function that acts like Excel’s 𝚁𝙰𝙽𝙳𝙱𝙴𝚃𝚆𝙴𝙴𝙽.

A function produces well-defined output for some given input. Recall that the library numpy contains the elementary functions found on a scientific calculator (and many more). The power of Python, however, lies in the ability to create our own functions.

The beginning of a function definition begins with the command def. Here’s an example, where we define a new function ln that returns the natural logarithm of an input x:

PythonCommentsdef ln(x):defines the funciton name and input(s)    y = log(x)compute the natural log of the input    return yreturn the result

Including a line that loads numpy, enter the above into IDLE, and run the module. If you execute ln(3) in the shell, you’ll get the same thing as log(3). Figure 5.13 shows the results:

Figure 5.13: ln Function

Some more comments:

  1. 1.

    In Python, lines of code are grouped by indentations of four spaces. The editor IDLE will automatically indent, easing some work on entering commands. Once a command is indented by an amount less than the prior, the grouping is finished. This will be illustrated in Section 5.4.

  2. 2.

    If you indent by a non-multiple of four, you will get a syntax error. You’ll get the hang of this after some trial and error.

  3. 3.

    The colon indicates the beginning of the grouped code within the function definition. If the colon isn’t there, IDLE won’t automatically indent, and when you run the module, you’ll get a syntax error.

  4. 4.

    The print command can be used to display some activity within a function, but return is special in that when it is executed, the function exits and returns the value to right of return.

5.3.1 randbetween

A function from numpy that we will use frequently generates (pseudo) random numbers similar in fashion to Excel’s 𝚁𝙰𝙽𝙳𝙱𝙴𝚃𝚆𝙴𝙴𝙽. The command, random.randint, works as follows: If m and n are integers with m < n, then random.randint(m,n) will generate a random integer with

𝚖random.randint(m,n)<𝚗,

with each possible integer equally likely. Note that the inequality on the right is strict. For example, executing random.randint(0,2) will generate 0’s and 1’s, with each value equally likely. The strict inequality on the right is annoying, but we can define a new function randbetween that behaves exactly like Excel’s as follows:

PythonComments def randbetween(m,n):the function takes two inputs    x = random.randint(m,n+1)increase the second input by 1    return x

Figure 5.14 illustrates the function:

Figure 5.14: randbetween

Note: The second def command in Figure 5.14 is to the left of the return command, and hence, it is not grouped within the prior def command.

5.3.2 Exercises

  1. 1.

    Answer the following as True or False.

    1. (a)

      The command to start a function is def.

    2. (b)

      The command return returns a value back to the Shell.

    3. (c)

      A script can only have one function.

    4. (d)

      The function randbetween created in the example above is meant to mimic the command 𝚁𝙰𝙽𝙳 in Excel.

    5. (e)

      The command randint(0,3) will generate 0’s, 1’s, 2’s, and 3’s.

  2. 2.

    In IDLE, write a function that takes input x and returns x2.

  3. 3.

    In IDLE, write a function that does the following:

    1. (a)

      Uses input to ask the user for a number.

    2. (b)

      Uses input to ask the user for another number.

    3. (c)

      Returns the product of the two numbers.

  4. 4.

    Create a function that takes the radius of a circle and returns the area of a circle. Recall the area of a circle is A=πr2, where r is the radius of the circle and π is the numeric value of pi. You will need numpy to call pi.