1 of 37

Functions:

Definitions

Jake Shoudy

Sept 2, 2020

CSCI 110 - Lecture 10

2 of 37

Announcements

3 of 37

Pre-class Surveys

Two surveys posted on Ed

Please take them 🙏 🙏 🙏

4 of 37

Reminder: Project 1: Part 2

Due Sunday (9/4) at 11:59pm! (No late days allowed!)

5 of 37

Reminder: Labor Day

NO CLASS ON MONDAY!

6 of 37

HW3

Posted yesterday

Due next Thursday (9/8) at 11:59pm!

7 of 37

Reminder: Exam 1

Wednesday after next! (9/14)

8 of 37

Recap

9 of 37

Review: Functions

What are they?

Block of reusable code that has three things:

  1. Name - used to call the function
  2. Parameters - used to pass values into a function (0, 1, or many)
  3. Return value - used to give values back the the caller (0 or 1)

Why are they important?

Reuse code without having to copy and paste

10 of 37

Calling Functions

choice = input('Name?')

Function name

Parameter

Parentheses

Return Value

11 of 37

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)

12 of 37

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

13 of 37

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

https://docs.python.org/3/library/math.html

14 of 37

Flip a Coin

from random import randint

number = randint(1,2)

if number == 1:

print('Heads')

else:

print('Tails')

15 of 37

Defining Functions

16 of 37

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

17 of 37

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”)

18 of 37

potential_password = input(“Set your password: ”)

if is_good_password(potential_password):

print(“Good password”)

else:

print(“Bad password”)

19 of 37

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

20 of 37

Function: Defining A Function

Name the function

def my_function(parameter1, parameter2):

# do some task using parameter1 and parameter2

21 of 37

Function: Defining A Function

Name the parameter(s) to do the task

def my_function(parameter1, parameter2):

# do some task using parameter1 and parameter2

22 of 37

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')

23 of 37

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

24 of 37

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”)

25 of 37

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

26 of 37

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”)

27 of 37

Variables vs Functions

Variables memorize data, give name to values

Functions memorize code, give name to a block of code

28 of 37

Defining and Returning

def is_even(number):

if number % 2 == 0:

return True

print("Even number!")

else:

return False

29 of 37

Defining and Returning

is_even(24680)

What can we do with

is_even(24680)?

print(is_even(24680))

store_as_var = is_even(24680)

30 of 37

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

31 of 37

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

32 of 37

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

33 of 37

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

34 of 37

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

35 of 37

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)

36 of 37

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

37 of 37

Questions?