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.


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.


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.


The int() function is used to convert strings of digits to integers.


The str() function is used to convert other data types to a string type and return it.


The float() function is used to convert strings of digits that are valid representations of floating point numbers into values of type float.

age = input("Enter your age: ")
# notice that it is a string
# convert age to an int
age = int(age)  
# input the radius of a circle
radius = input("Enter radius value: ") 
# currently stored as a string

# convert radius to float
radius = float(input("Enter radius value: ")) 
# now radius is a float
pi = 3.14159
# comma adds space when printing
print("Area of circle with radius", radius, "is", pi * (radius**2)) 
# convert 199 to str type
# converts None to str type
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:


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.



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(someNumber):
    return someNumber * someNumber

The following function call will generate the same result as the one from the definition of the function with a different parameter name.


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?
NameError                                 Traceback (most recent call last)
Input In [20], in <module>
      1 # what does this return?
----> 2 someNumber

NameError: name 'someNumber' 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 totalSecs(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
totalSecs(3, 67) 

Why use docstrings? To help the user of the function understand the intended use.

# help is another in-built function that prints docstring!
Help on function totalSecs in module __main__:

totalSecs(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 invocation

  • print() 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 printExp(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)
printExp(3, 2)
3 raised to exponent 2 is 9

Question. Will this work? What does printExp() return?

printExp(3, 1) + printExp(4, 2)
3 raised to exponent 1 is 3
4 raised to exponent 2 is 16
TypeError                                 Traceback (most recent call last)
Input In [31], in <module>
----> 1 printExp(3, 1) + printExp(4, 2)

TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType'
type(printExp(3, 2))
3 raised to exponent 2 is 9
print(printExp(3, 2))
3 raised to exponent 2 is 9

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 numCoins(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"""
  # TODO
Help on function numCoins in module __main__:

    Takes as input cents and returns the fewest number of coins of type
    quarter, dimes, nickels and pennies that can make change for cents