CP104 Functions - Chapter 5
A function is a group of statements that exist within a program for the purpose of
performing a specific task.
Def function_name ():
Statement
Statement
Statement
Etc
# This program demonstrates a function.
# First, we define a function named message.
def message():
print('I am Arthur,') print('King of the Britons.')
# Call the message function.
message()
Program Output
I am Arthur,
King of the Britons
(two functions)
# This program has two functions. First we # define the main function.
def main():
print('I have a message for you.')
message()
print('Goodbye!')
# Next we define the message function.
def message():
print('I am Arthur,') print('King of the Britons.')
# Call the main function.
main()
Program Output
I have a message for you.
I am Arthur,
King of the Britons.
Goodbye!
Passing Arguments to Functions
An argument is any piece of data that is passed into a function when the function is called. A
parameter is a variable that receives an argument that is passed into a function.
# This program demonstrates an argument being # passed to a function.
def main():
value = 5
show_double(value)
# The show_double function accepts an argument # and displays double its value.
def show_double(number):
result = number * 2
print(result)
# Call the main function.
main()
Program Output
10
Passing Multiple Arguments
This program demonstrates a function that accepts # two arguments.
def main():
print('The sum of 12 and 45 is')
show_sum(12, 45)
# The show_sum function accepts two arguments # and displays their sum.
def show_sum(num1, num2):
result = num1 + num2 print(result)
# Call the main function.
main()
Program Output
The sum of 12 and 45 is
57
Generating Random Numbers
number = random.randint (1, 100)
Random Numbers
# This program displays five random
# numbers in the range of 1 through 100.
import random
def main():
for count in range(5):
# Get a random number.
number = random.randint(1, 100)
# Display the number.
print(number)
# Call the main function.
main()
Program Output
89 7 16 41 12
DICE
# This program the rolling of dice.
import random
#Constants for the minimum and maximum random numbers
MIN = 1
MAX = 6
main():
# Create a variable to control the loop.
again = 'y'
# Simulate rolling the dice.
while again == 'y' or again == 'Y':
print('Rolling the dice ...')
print('Their values are:')
print(random.randint(MIN, MAX))
print(random.randint(MIN, MAX))
# Do another roll of the dice?
again = input('Roll them again? (y = yes): ')
# Call the main function.
main()
The Math Module
The Python standard library’s math module contains numerous functions that can be
used in mathematical calculations.
result = math.sqrt(16)
SQUARE ROOT
# This program demonstrates the sqrt function. import math
def main():
# Get a number.
number = float(input('Enter a number: '))
# Get the square root of the number.
square_root = math.sqrt(number)
# Display the square root.
print(f'The square root of {number} is {square_root}.')
# Call the main function. main()
Program Output (with input shown in bold)
Enter a number: 25 Enter
The square root of 25.0 is 5.0.
MATH MODULE OPERATORS
acos(x)
Returns the arc cosine of x, in radians.
Asin (x)
Returns the arc sine of x, in radians.
Atan (x)
Returns the arc tangent of x, in radians.
ceil(x) Returns the smallest integer that is greater
than or equal to x.
cos(x)
Returns the cosine of x in radians.
degrees(x)
Assuming x is an angle in radians, the
function returns the angle converted to
degrees.
exp(x)
Returns ex
Floor (x)
Returns the largest integer that is less than or
equal to x.
hypot(x,y)
Returns the length of a hypotenuse
that extends from (0, 0) to (x, y).
log(x)
Returns the natural logarithm of x.
log10(x)
Returns the base-10 logarithm of x.
radians(x)
Assuming x is an angle in degrees, the
function returns the angle converted to
radians.
sin(x) Returns the sine of x in radians.
sqrt(x)
Returns the square root of x.
tan(x) Returns the tangent of x in radians.
The math.pi and math.e Values
The math module also defines two variables, pi and e, which are assigned mathematical values
for pi and e.
area = math.pi * radius**2