Functions¶
In this notebook, we will discuss built-in and user-defined functions in Python, as shown under the following topics:
Built-in functions
Defining your own functions
Multiple parameters
return vs. print
Variable Scope
Built-in Functions: input()
, print()
, int()
, float()
, str()
¶
Python comes with several built-in capabilities. In this section, we will explore some useful built-in functions.
print()
¶
The print()
function is used to display characters on the screen. When we print something, notice that there is no output (no Out[] cell). This is because it does not return a value as an output (like a Python expression). It simply performs an action.
input()
¶
The input()
function is used to take input from the user. We specify the prompt we want the user to see within the parentheses. By default, input values are always of type string.
int()
¶
The int()
function is used to convert strings of digits to integers.
age = input("Enter your age: ")
# notice that it is a string
age
'19'
# convert age to an int
age = int(age)
age
19
# input the radius of a circle
radius = input("Enter radius value: ")
# currently stored as a string
radius
'2.2'
# convert radius to float
radius = float(input("Enter radius value: "))
# now radius is a float
radius
2.2
pi = 3.14159
# comma adds space when printing
print("Area of circle with radius", radius, "is", pi * (radius**2))
# convert 199 to str type
str(199)
'199'
# converts None to str type
str(None)
'None'
dollars = 10
# can also concatenate strings with +
print('The burrito costs $' + str(dollars) + '.')
The burrito costs $10.
Thought Question. Which of the functions among print()
, input()
, int()
return an explicit value as output?
What does the print()
function return? print()
doesn't explicitly return anything and just displays the printed text on the screen.
It turns out that calling print()
actually returns the special None
value. Python uses a None return value to indicate the function was called to perform an action when there is no explicit return value.
To emphasize that calls to print()
return None
, try out the following piece of code:
str(print("Hello!"))
Hello!
'None'
Defining your own functions¶
Functions are a great way of abstracting and reusing useful blocks of code. We have already used built-in functions like print()
, input()
and int()
. We can define our own functions as shown in the examples below.
def square(num):
'''A simple user-defined function.'''
return int(num * num)
Calling or invoking the function: We can call a function many times, but we only define it once.
square(5.5)
30
square(7)
49
Parameters
Parameter names are "holes" in the body of our function that will be filled in with the argument value for each invocation.
The particular name we use for a parameter is irrelevant, as long as we use the name consistently in the body.
def square(some_number):
return some_number * some_number
The following function call will generate the same result as the one from the definition of the function with a different parameter name.
square(5)
25
Function parameters only have meaning within the body of the function. We will come back to this concept when we discuss variable scope.
# what does this return?
some_number
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[19], line 2 1 # what does this return? ----> 2 some_number NameError: name 'some_number' is not defined
Multiple parameters¶
A function can take as many parameters as needed. They are listed one by one, separated by commas.
Important (Order matters!) The order of parameters specifies the order of argument values.
def total_secs(mins, secs):
'''Given the parameters mins and secs, return total number of seconds'''
return (mins * 60) + secs
If we call the function and provide the arguments in the wrong order (for example, secs
first and then mins
), it will not work as intended. The order of arguments in the function call should be the same as the order of the corresponding parameters in the function definition.
Call the function:
# 3 minutes, 67 seconds
total_secs(3, 67)
247
Why use docstrings? To help the user of the function understand the intended use.
# help is another in-built function that prints docstring!
help(total_secs)
Help on function total_secs in module __main__: total_secs(mins, secs) Given the parameters mins and secs, return total number of seconds
Function calling Functions¶
Once you have defined a function, you can call it from within other functions.
def song(action):
return "If you are happy and you know it, " + action
def singsong():
print(song("clap your hands"))
print(song("stomp your feet"))
print(song("snap your fingers!"))
In the above example, singsong()
is a zero-parameter function which is calling
the function print()
, which is calling the function song()
.
Note The function song()
returns a string. The returned string replaces the function call within the print()
, which results in it being printed.
Excercise: Write function average()
¶
Let us define a function named average()
that takes two numbers and returns the average of the two.
def average(x,y):
return (x + y) / 2
Now try calling your function below:
average(6, 16)
average(5, 7)
return
vs. print()
¶
return
specifies the result of the function invocationprint()
causes characters to be displayed in the shell.- They are very different!
def exp(num, k):
"""Return the kth power of given number num"""
return num**k
def print_exp(num, k):
"""Print the kth power of given number num"""
print("{} raised to exponent {} is {}".format(num, k, num**k))
Try out the result of the following expressions:
exp(3, 2) + exp(2, 3)
print_exp(3, 2)
Question. Will this work? What does print_exp()
return?
print_exp(3, 1) + print_exp(4, 2)
type(print_exp(3, 2))
print(print_exp(3, 2))
Make Change Example¶
Problem. Suppose you are a cashier and you need to make change for a given number of cents using only quarters, dimes, nickels, and pennies.
Most cashiers use the following greedy strategy to make change using the fewest number of coins: use as many quarters as possible first, then as many dimes as possible next, and so on, using the fewest number of pennies last. Assume you have an unlimited supply of each coin.
# simple function to make change
def num_coins(cents):
"""Takes as input cents and returns the fewest number of coins of type
quarter, dimes, nickels and pennies that can make change for cents"""
pass
# TODO
help(num_coins)