4 Functions
4 Functions
Function calls
• In the context of programming, a function is a named sequence of
statements that performs a computation.
• This statement creates a module object named math. If you print the
module object, you get some information about it:
Math functions
• The module object contains the functions and variables defined in
the module. To access one of the functions, you have to specify the
name of the module and the name of the function, separated by a
dot (also known as a period). This format is called dot notation.
Math functions
• The first example computes the logarithm base 10 of the signal-to-
noise ratio. The math module also provides a function called log that
computes logarithms base e.
• The second example finds the sine of radians. The name of the
variable is a hint that sin and the other trigonometric functions (cos,
tan, etc.) take arguments in radians.
Math functions
• To convert from degrees to radians, divide by 360 and multiply by 2𝜋:
• The expression math.pi gets the variable pi from the math module. If
you know your trigonometry, you can check the previous result by
comparing it to the square root of two divided by two:
Adding new functions
• It is also possible to add new functions. A function definition
specifies the name of a new function and the sequence of
statements that execute when the function is called.
• Once we define a function, we can reuse the function over and over
throughout our program.
Adding new functions
• def is a keyword that indicates that this is a function definition. The
name of the function is print_lyrics.
• The rules for function names are the same as for variable names:
letters, numbers and some punctuation marks are legal, but the first
character can’t be a number. You can’t use a keyword as the name of
a function, and you should avoid having a variable and a function
with the same name.
• The empty parentheses after the name indicate that this function
doesn’t take any arguments.
Adding new functions
• The first line of the function definition is called the header; the rest is
called the body. The header has to end with a colon and the body
has to be indented.
• The body can contain any number of statements.
• The strings in the print statements are enclosed in quotes. Single
quotes and double quotes do the same thing; most people use single
quotes except in cases like this where a single quote (which is also an
apostrophe) appears in the string.
Adding new functions
• Defining a function creates a variable with the same name.
• The value None is not the same as the string “None”. It is a special
value that has its own type.
Fruitful functions and void functions
• To return a result from a function, we use the return statement in
our function. For example, we could make a very simple function
called addtwo that adds two numbers together and returns a result.
• When this script executes, the print statement will print out “8”
because the addtwo function was called with 3 and 5 as arguments.
Why functions?
• Creating a new function gives you an opportunity to name a group of
statements, which makes your program easier to read, understand,
and debug.
• Functions can make a program smaller by eliminating repetitive code.
Later, if you make a change, you only have to make it in one place.
• Dividing a long program into functions allows you to debug the parts
one at a time and then assemble them into a working whole.
• Well-designed functions are often useful for many programs. Once
you write and debug one, you can reuse it.
Exercises
• Exercise 4.1 - Rewrite your pay computation with time-and-a-half for
overtime and create a function called computepay which takes two
parameters (hours and rate).
• Exercise 4.2 - Rewrite the grade program from the previous chapter
using a function called computegrade that takes a score as its
parameter and returns a grade as a string.
Exercises
• Exercise 4.3 - Rewrite the triangle program using a function called
triangle that takes the length of the 3 sides of a triangle as its
parameters and returns its type (equilateral, isosceles or scalene).