CS105 Study Guide

Unit 5: Functions

5a. Analyze situations where using functions could improve a program

  • How are functions used?
  • When should a function be created?
  • What is the difference between a user-defined function and a built-in function?

A function is created when a given task is performed or a set of instructions are utilized at several different points throughout a program. If this happens, you should consider wrapping the task or set of instructions into a function or a method. Functions and methods can be called and used over and over again at any location within a program without having to reproduce the code. The code resides in the function definition. All that needs to happen is to execute an instruction that refers to or 'calls' the function. Using functions in this way makes programs more readable and more efficient.

There are two main types of functions: built-in and user-defined. Python built-in functions are provided as part of the language. Functions such as min, max, sum, and len are examples of built-in functions. User-defined functions are written by the programmer and require adhering to the rules of the Python language syntax which makes provision for creating new functions.

To review, see Creating Functions.


5b. Explain how functions are defined and how to use function syntax

  • What are the syntax rules for creating a function?
  • What are the syntax rules for calling a function?
  • What Python instruction allows for providing output from the instruction?

There are specific rules for creating user-defined functions:

  • The Python keyword def is used to indicate the definition line of a function.
  • def is followed by the name of the function.
  • Input variables are separated by a comma and contained within parentheses following the function name.
  • If there are no input variables, then left-right parentheses must still be used with no arguments in between.
  • The right parenthesis is followed by a colon (similar to if and loop statements).
  • Instructions contained within a function are indented and obey typical rules for indentation.
  • Any data to be output by the function is made available to the calling function using a return

There are specific rules for calling a function:

  • The function name must be used.
  • Input variables are contained within parentheses following the function name and are separated by a comma.
  • If the function requires no input variables, then left-right parentheses must still be used with no arguments in between.

To review, check all examples of functions introduced in this course to be sure that the above rules have been adhered to and you are completely certain of their application.

To review, see Creating Functions.


5c. Use functions to better organize programs written in Units 2-4

  • What are some examples of user-defined functions applicable to Units 2-4?
  • What are some algorithms identified in these units that can be phrased within a function?
  • Why are programs better organized after phrasing code introduced in Units 2-4 within a function?

The three major examples of algorithms we introduced were finding the minimum value contained within a numerical list, finding the maximum value contained within a numerical list, and summing the values contained within a numerical list. Python already has built-in functions to perform these operations (min, max, and sum). However, it is best to know how to create user-defined functions to perform these operations in the event that you need to modify them in some way. They also are simple examples for learning how to program.

After you practice with the syntax for creating such user-defined functions, it should be clear that it is much more efficient to have, for example, a callable sum instruction. Such an instruction can be called anywhere within a program. The need to reproduce the actual code several times is eliminated. This is the beauty of creating user-defined functions. Programmers are given the power to create their own user-defined, reusable instructions that enable clearer program organization.

To review, see Creating Functions.


5d. Apply functions and methods available from the Python math library

  • What is the syntax for invoking the math module?
  • What are some useful methods contained within the math module?
  • What is the syntax for calling a method from the math module?

The math module contains a host of methods necessary for computation. Invoking the math module requires the import math instruction. After this, methods can be called using the dot notation. For example, math.sqrt(5), math.log10(50), and math.cos(math.pi) are all valid method calls.

The math module can be used to make computations similar to that of a calculator such as trigonometric functions, square roots, powers, exponentials, and logarithms. As part of your basic skill set, you must be able to take a simple equation and translate it into Python code by applying methods from the math module. The basic guiding premise is that, if a calculation can be entered into a calculator, it is possible to translate it into Python code.

To review, see Trigonometry Review and The "math" Module.


5e. Apply functions and methods for generating and applying random numbers

  • What is the syntax for invoking the random module?
  • What are some useful methods contained within the random module?
  • What is the syntax for calling a method from the random module?

The random module is useful to this end and can be invoked using the import random instruction. As with other Python modules, method calls follow the same dot notation convention used in object-oriented programming. Examples of random method calls are random.seed(30), random.random() , random.randint(0,20) and random.shuffle(z) (where z is a list). In order to shorten the instruction length, the random module is often imported using the as qualifier:

import random as rn

The previous examples would then look like: rn.seed(30), rn.random() , rn.randint(0,20) and rn.shuffle(z).

The random module can be used to generate random numbers for simulating random events. The most common model at the beginner level is the uniform random distribution, which can be applied using the random method. Many more advanced distributions are also available such as the normal distribution (or Gaussian distribution) using the gauss method. It is also possible to generate random integers over a specified range using the randint method. Another useful method is shuffle, which can randomly shuffle elements contained within a list. Advanced understanding of probability and statistics not addressed in this course will allow you to explore deeper programming aspects of the random module.

To review, see Random Numbers.


Unit 5 Vocabulary

This vocabulary list includes terms listed above that students need to know to successfully complete the final exam for the course.

  • function
  • built-in functions
  • user-defined functions
  • def
  • input variables 
  • indentation
  • output
  • return
  • import
  • math module
  • random module
  • uniform random distribution
  • normal distribution (or Gaussian distribution)