Functions:
Definitions
Jake Shoudy
Sept 2, 2020
CSCI 110 - Lecture 10
Announcements
Pre-class Surveys
Two surveys posted on Ed
Please take them 🙏 🙏 🙏
Reminder: Project 1: Part 2
Due Sunday (9/4) at 11:59pm! (No late days allowed!)
Reminder: Labor Day
NO CLASS ON MONDAY!
HW3
Posted yesterday
Due next Thursday (9/8) at 11:59pm!
Reminder: Exam 1
Wednesday after next! (9/14)
Recap
Review: Functions
What are they?
Block of reusable code that has three things:
Why are they important?
Reuse code without having to copy and paste
Calling Functions
choice = input('Name?')
Function name
Parameter
Parentheses
Return Value
Common Confusions
call != define
call != print
return != print
print is just a function
print can still happen inside functions!
every function has a return value (or None)
Review: Modules and Imports
What?
module: set of functions written and shared by another programmer
import statement: loads module into program to make its functions available
Why?
Use other people’s code, write code for others to use
Code organization - split project up into multiple files
import math
Access mathematical utilities
math.ceil(3.14) == 4
math.factorial(4) == 24
math.sqrt(25) == 5
math.cos(2*math.pi) == 1.0
Flip a Coin
from random import randint
number = randint(1,2)
if number == 1:
print('Heads')
else:
print('Tails')
Defining Functions
What are Functions?
Give name to a block of code
Reuse code without having to copy and paste it
Each function should be responsible for some specific functionality
Performs specific task and simplify code
potential_password = input(“Set your password: ”)
has_capital = False
has_special = False
has_number = False
index = 0
while index < len(potential_password):
if potential_password[index] >= ‘A’ and potential_password[index] <= ‘Z’:
has_capital = True
elif potential_password[index] == ‘&’ or potential_password[index] == ‘?’:
has_special = True
elif potential_password[index] >= ‘0’ and potential_password[index] <= ‘9’:
has_number = True
index += 1
if has_capital and has_special and has_number:
print(“Good password”)
else:
print(“Bad password”)
potential_password = input(“Set your password: ”)
if is_good_password(potential_password):
print(“Good password”)
else:
print(“Bad password”)
Function: Defining A Function
Define a function to reuse again and again using def
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
Function: Defining A Function
Name the function
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
Function: Defining A Function
Name the parameter(s) to do the task
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
Return Value
Return statement: gives back (returns) value to caller of function
Function stops running after return statement, even if more code after
def my_function(parameter1, parameter2):
# do some task using parameter1 and parameter2
print('This will print')
return something
print('This will NOT print')
Return Value
Function can have multiple return statements but only one will ever run
def my_function(argument1, argument2):
if some statement is True:
return True
else:
return False
potential_password = input(“Set your password: ”)
has_capital = False
has_special = False
has_number = False
index = 0
while index < len(potential_password):
if potential_password[index] >= ‘A’ and potential_password[index] <= ‘Z’:
has_capital = True
elif potential_password[index] == ‘&’ or potential_password[index] == ‘?’:
has_special = True
elif potential_password[index] >= ‘0’ and potential_password[index] <= ‘9’:
has_number = True
index += 1
if has_capital and has_special and has_number:
print(“Good password”)
else:
print(“Bad password”)
def is_good_password(password):
has_capital = False
has_special = False
has_number = False
index = 0
while index < len(password):
if password[index] >= ‘A’ and password[index] <= ‘Z’:
has_capital = True
elif password[index] == ‘&’ or password[index] == ‘?’:
has_special = True
elif password[index] >= ‘0’ and password[index] <= ‘9’:
has_number = True
index += 1
if has_capital and has_special and has_number:
return True
else:
return False
password_1 = input(“Set your password: ”)
password_2 = input(“Try another one: ”)
if is_good_password(password_1) and is_good_password(password_2):
print(“Both passwords are good!”)
else:
print(“At least one of your passwords is bad”)
Variables vs Functions
Variables memorize data, give name to values
Functions memorize code, give name to a block of code
Defining and Returning
def is_even(number):
if number % 2 == 0:
return True
print("Even number!")
else:
return False
Defining and Returning
is_even(24680)
What can we do with
is_even(24680)?
print(is_even(24680))
store_as_var = is_even(24680)
Function: Absolute Value
if num < 0:
answer = -num
else:
answer = num
def abs(num):
if num < 0:
answer = -num
else:
answer = num
return answer
function name
Passing arguments
result = abs(-8)
other_result = abs(2)
def abs(num):
if num < 0:
answer = -num
else:
answer = num
return answer
num = -8
answer = 8
result = 8
Passing arguments
result = abs(-8)
other_result = abs(2)
def abs(num):
if num < 0:
answer = -num
else:
answer = num
return answer
num = 2
answer = 2
other_result = 2
Arguments vs Parameters
result = abs(-8)
other_result = abs(2)
def abs(num):
if num < 0:
answer = -num
else:
answer = num
return answer
argument
parameter
Argument vs Parameter
argument: value provided as input to the function in function call
parameter: named variable representing input in the function definition
result = abs(-8)
other_result = abs(2)
def abs(num):
# code
argument
parameter
Common Confusions
call != define
call != print
return != print
print is just a function
print can still happen inside functions!
every function has a return value (or None)
Review: Functions
What are they?
Block of reusable code with a name
Call function to run it
Pass arguments to function so function can do its job
Define function using def to give a name to block of code
Return a value from function, can print value or store in variable
Why are they important?
Reuse code without having to copy and paste
Questions?