Functions

CSCI 1012

Announcements

Q: What’s going to be on the quiz?
A: Everything through the last homework

In-Class Exercises

  • We’ll walk through the solutions
    • Try them yourself
  • Turn in on the submit server for credit
    • Only open during the lecture
    • Can’t turn in late
  • Turn in one .zip with all the .py files
    • Unlimited attempts during lecture

Expressions

“Small units of computation”

# this is a comment
1 + 3


What was printed?


nothing

Printing

The print function

# this is a comment
print(1 + 3)
4

Assignment

  • Expression on right of = evaluated
  • Variable on left of = gets result
x = 1 + 3
print(x)
4

Reassignment:

x = 1 + 3
print(x)
x = x * 2
print(x)
4
8

Operators

Operator Function (with numbers)
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
// Quotient
% Remainder

Operators

They behave differently with text:

x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)

Operators

They behave differently with text:

x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
3

Operators

They behave differently with text:

x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
3

Operators

They behave differently with text:

x = 1 + 2
print(x)
y = "h" + "arbor"
print(y)
3
harbor

Exercise 1

This code should:

  • Add 3 and 4 and assign to y
  • Multiply y by 5
  • Print out the current value of y
  • Then, add 1 to y
  • Print out the final y

Desired output:

7
35
36
lecture3_01.py
# 27 Jan 2025 - In Class
y = 3 + 4
print(y * 5)
y = y + 1
print(y)

Submit on the submit server!

Functions

Anatomy of a print() statement:

  • The name print
  • Parentheses ()
  • Argument - what is inside the parentheses

The print function is special:

  • It does something
  • It doesn’t return anything

print

x = 2
print(x)
y = print(x)
print(y)

Function Returns

  • Most functions return something after they run:
x = 5/4
print(x)
y = int(x)
print(y)
1.25
1
  • int function called on 1.25 (a float) returns 1 (an int)

Types!

  • Review Think Python Chapter 1 (1.5)
    • int - integer, whole numbers
    • float - numbers with decimal
    • str - “string” - text information
  • Functions int, float, and str convert between types
    • Some conversions are “impossible”
  • Function type tells us the type of a variable

Types

# Example 1
x = 2 + 2
y = float(x)
print(y)
4.0
# Example 2
a = 1 + 4
b = str(a) + str(a)
print(b)
55
# Example 3
s = "two"
t = int(s) + float(s)
print(t)

Error! Why?

Defining Functions

  • User-defined functions:
    • Reusable code blocks
def function_name():
  print("The function does something!")


Function with argument:

def function_name(arg):
  print("The argument is: " + str(arg))

Exercise 2

  • Write a function exercise_2
  • Your function should take an argument
  • Your function should print out "Today is January 27" on one line
  • Your function should print out the argument (by itself) on the next line

Desired output:

exercise_2("Hello!")
Today is January 27
Hello!
exercise_2(5 + 4)
Today is January 27
9
lecture3_02.py
# 27 Jan 2025 - In Class
def exercise_2(  ): # you need to add an argument
  # your code goes here
  # you probably want to use two print statements

Submit on the submit server!

Function Return

  • We usually want our functions to give us something back

Math:

\(f(x) = x + 1\)

 

Python:

def f(x):
  return x + 1

Definition vs. Call

Definition:

def my_func(my_arg):
  my_arg = my_arg + 1
  return my_arg + 1

result = my_func(2)
print(result)
4

Execution Order

Code in the visualizer

def f(x):
  return x + 1

a = 3
b = f(a)
print(b)
c = f(b)
print(c)
4
5


Exercise 3

  • Write a function exercise_3
  • Your function should take an integer argument
  • Your function should repeat the argument twice
    • 2 repeated becomes 22
    • 12 repeated becomes 1212
  • Your function should return the result – an integer

Desired output:

x = exercise_3(5)
print(x)
55
y = exercise_3(21)
print(y)
2121
exercise_3(2)

(nothing is printed)

lecture3_03.py
# 27 Jan 2025 - In Class
def exercise_3(  ): # you need to add an argument
  return # write the statement to return here

Submit on the submit server!

Next Week

Reading: Think Python Chapter 5

Homework 3 due Sunday, 2 Feb 11:55 PM

  • 6 attempts!