Functions

Joel Ross
Spring 2025

LIS 511

Questions?

What should we go over?

Functions

A named sequence of instructions (lines of code). We call a function to do those steps.

Functions abstract computer programs!

print("Hello world")

function name

argument (value)

len("Hello") # returns 5

Arguments

# prints "Hello+++World"
print("Hello", "World", sep = "+++")

multiple arguments are separated by commas

keyword argument

# rounds 5/7 to the nearest .01
round(5/7, 2)  # 0.71

Argument order (position) usually matters

Function arguments are the "inputs".

expressions in args are evaluated before the function is executed

Return Values

Functions may return a value (the "output"). This value must be stored in a variable for the machine to use later!

# store min value in smallest_number variable
smallest_number = min(1, 6/8, 4/3, 5+9)  # 0.75

# use the variable as normal, such as for math
twice_min = smallest_number * 2  # 1.5

# use functions directly in expressions
# (the returned value is "anonymous")
number = .5 * round(9.8)  # 5.0

# pass the result of a function as an arg to another!
# (`abs` is "absolute value")
# watch out for where the parentheses close!
print(min(2.0, abs(-3)))  # prints 2.0

Text

Dot Notation

We call a method on a data value by using dot notation, putting the data value, then a dot ( .), then the function call.

message = "Hello World"

# call the lower() method on the message
# original string does not change
lower_message = message.lower()  # "hello world"

# call the replace() method on the message
western_message = message.replace("Hello", "Howdy")  
## "Howdy World"

The dot is like an 's in English: "execute message's lower()"

Exercise: Calling Functions

Your Turn!

Modules

Python functions are organized into modules, which need to be individually loaded. This helps reduce memory usage (only for functions you actually need).

# load the math module (contains math functions)
import math

# call the math module's sqrt() function
math.sqrt(25) # 5.0, (square root of 25)

# print out the math module's `pi` variable
print(math.pi) # 3.141592653589793

# import specific function, available globally
from math import gcd

# call gcd function (greatest common denominator)
gcd(56, 42)

Your Turn!

Exercise: Using Module Functions

Defining a Function

# A function that says hello to someone
def say_hello(name):
    greeting = "Hello " + name
    print(greeting)

say_hello("Joel")
say_hello("class")
# general syntax
def function_name(my_arg):
    # body: statements (code) go here

optional, comma-separated

Indented (4 spaces OR 1 tab)

colon
(start block)

abstraction!

Function Arguments

Arguments are variables (labels) that are assigned values when the function is called.

name = "Joel" #implicit
def say_hello(name):
    greeting = "Hello " + name
    print(greeting)



say_hello("Joel")

Keyword Arguments

Give a function a keyword argument by defining a default value for that argument variable:

# includes a single keyword argument
def greet(greeting = "Hello"):
    print(greeting + " world")

# call by assigning to the arg
greet(greeting = "Hi")  # "Hi world"

# call without an arg to use the default value
greet()  # "Hello world"

Return Values

Functions can return a single value as a result. This is different than printing an output.

def make_full_name(first_name, last_name):
    full_name = first_name + " " + last_name
    return full_name



my_full_name = make_full_name("Joel", "Ross")

Remember to give the result
a label to use it later!

"return" the value
This exits the function

Your Turn!

Exercise: Writing Functions

Action Items!

  • Keep working on Module 1
    • Try to finish all exercises by next week!

Next time: More work time