1 of 47

Intro to Functions

1

CS 110: Intro to Computer

Programming with Python

2 of 47

Outline

  1. Statements & Expressions
  2. Intro to functions
  3. Built-in functions

2

3 of 47

Statements & Expressions

  1. An expression is any section of the code that evaluates to a value
  2. A statement is a complete line of code that performs some action

3

4 of 47

Examples of Expressions

3 * 7

1 + 2 + 3 * (8 ** 9) - sqrt(4.0)

max(3, 94)

my_var

"bar"

my_var + "bar"

None

True

�Whether constants, operations, accessing values in variables, or function calls, these expressions all evaluate to a value (i.e. a piece of data that can be stored and manipulated)

4

5 of 47

Examples of Statements

a = 3

def my_function():

while CONDITION:

if CONDITION:

for VARIABLE in SEQUENCE:

Statements perform actions but don’t evaluate to a value (i.e. you couldn’t store the results in a variable). We will talk more about statements next week (we haven’t gone over all of these yet).

5

6 of 47

Outline

  • Statements & Expressions
  • Intro to functions
  • Built-in functions

6

7 of 47

Intro to Functions: Outline

  1. Functions are similar to operators, but more flexible
  2. Functions are a type of expression, often called a “call expression.”
  3. They provide a way to encapsulate, organize, and reuse code
  4. To “run” a function you have to call it or invoke it (using parentheses)
  5. Functions can have optional or required inputs (parameters/arguments), and an optional output value
  6. Some functions are built-in. Others can be imported from modules. You can also make your own!

7

8 of 47

1. Functions are similar to operators, but more flexible

Recall: when we examined arithmetic operators...

  1. The operator referred to the operation we wanted to perform (addition, multiplication, subtraction, etc.)
  2. The operands were the data we wanted to perform the operation on
  3. The result of the operation on the data was stored in a variable (or printed directly to the screen)

8

9 of 47

Example 1: Addition

9

4 + 6

10 of 47

Example 1: Addition

10

4 + 6

operand

operand

operator

11 of 47

Example 1: Addition

11

4 + 6

4

operand

6

operand

+

operator

12 of 47

Example 1: Addition

12

4 + 6

10

4

operand

6

operand

+

operator

13 of 47

Example 2: Multiplication

13

4 * 6

14 of 47

Example 2: Multiplication

14

4 * 6

operand

operand

operator

15 of 47

Example 2: Multiplication

15

4 * 6

4

operand

6

operand

*

operator

16 of 47

Example 2: Multiplication

16

4 * 6

24

4

operand

6

operand

*

operator

17 of 47

Example 3: Multiplication using function notation...

17

mul(4, 6)

18 of 47

Example 3: Multiplication using function notation...

18

mul(4, 6)

argument

argument

function name

19 of 47

Example 3: Multiplication using function notation...

19

mul(4, 6)

mul

6

operand

4

argument

argument

function name

20 of 47

Example 3: Multiplication using function notation...

20

mul(4, 6)

24

mul

6

argument

4

function name

argument

Return value

21 of 47

1. Functions are similar to operators, but more flexible

operator → function name

operands → arguments�(usually 2) (zero or more)

result → return value

2 * 2 → mul(2, 2)

21

22 of 47

2. Functions are “call expressions”

22

23 of 47

2. Functions are “Call Expressions”

A call expression invokes a function, providing zero or more inputs, and eventually “returns.” Some call expressions (hereafter “function calls”) return values, others return None (no value).

3 is an expression (a constant)

1 + 2 is also an expression that evaluates to 3

add(1, 2) is a call expression that returns 3 as a result

add(-3, 6) is also a call expression that returns 3 as a result

All of these expressions produce the same value after they are evaluated: 3

23

24 of 47

What are the rules of the add function?

  • What can be an argument?
  • What data types does add return?

24

25 of 47

What are some legal and illegal arguments?

add(2, 2.5)

add(??, ??)

add(??, ??)

add(??, ??)

add(??, ??)

25

26 of 47

26

mul(add(2, mul(4, 6)), add(3, 5))

27 of 47

27

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

28 of 47

28

add(3, 5)

add(2, mul(4, 6))

mul

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

29 of 47

29

add(3, 5)

add(2, mul(4, 6))

mul

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

function

argument

argument

function

argument

argument

30 of 47

30

mul(4, 6)

add(3, 5)

add

5

3

add(2, mul(4, 6))

add

2

mul

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

function

argument

argument

function

argument

argument

31 of 47

31

mul(4, 6)

add(3, 5)

add

5

3

add(2, mul(4, 6))

add

2

mul

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

function

argument

argument

function

argument

argument

function

argument

argument

32 of 47

32

mul(4, 6)

mul

6

4

add(3, 5)

add

5

3

add(2, mul(4, 6))

add

2

mul

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

function

argument

argument

function

argument

argument

function

argument

argument

33 of 47

33

mul(4, 6)

mul

6

4

add(3, 5)

add

5

3

add(2, mul(4, 6))

24

add

2

mul

mul(add(2, mul(4, 6)), add(3, 5))

function

argument

argument

function

argument

argument

function

argument

argument

function

argument

argument

34 of 47

34

mul(4, 6)

mul

6

4

add(3, 5)

8

add

5

3

add(2, mul(4, 6))

24

add

2

mul

mul(add(2, mul(4, 6)), add(3, 5))

26

function

argument

argument

function

argument

argument

function

argument

argument

function

argument

argument

35 of 47

35

mul(4, 6)

mul

6

4

add(3, 5)

8

add

5

3

add(2, mul(4, 6))

24

add

2

mul

mul(add(2, mul(4, 6)), add(3, 5))

208

26

function

argument

argument

function

argument

argument

function

argument

argument

function

argument

argument

36 of 47

3. Functions provide a way to encapsulate and reuse code

  • Useful for encapsulating, abstracting, organizing, and reusing code
  • You don’t need to know how something works, just that it will always do what you want it to do
  • You can perform the same operations over and over using different data

36

max()�???

1, 99, 123, 3

123

max()�???

1, 34, 66, 3

66

37 of 47

4. To run a function, you have to call or “invoke” it

In order to run a function, you have to invoke it and give it the data it needs, using the correct type:

>>> max # tells you that max is a function

>>> max(2) # error (it expected a sequence or > 1 arg)

>>> max(2, 55, 29, 88, 7)�88

# interpreter confused: what are you asking me to do?

>>> max('5555', 55, [1, 2, 3])

37

38 of 47

5. Function Inputs: Parameters and Arguments

  • Some functions don’t accept any data/arguments
  • Some functions require certain kinds of data/arguments
  • Some functions allow you to pass in multiple optional data/arguments

38

39 of 47

Terminology: Parameters & Arguments

  • arguments: the data that you pass into a function
  • parameters: local variables, inside a function, that are assigned when the function is invoked
  • function definition: tells you which parameters are required and which are optional (if any)
  • positional parameters: parameters that required (i.e., they need arguments). Order matters.
  • keyword parameters: parameters that are optional. Order does not matter, but keyword parameters always come after positional parameters.

39

40 of 47

Positional Parameters

  1. Refers to a function’s REQUIRED parameters�
  2. Order matters: you have to pass data into the function in the precise order that the function “expects”

40

41 of 47

Positional Parameters: Example

Consider the built-in function pow(), which has 2 required positional parameters: (1) the base number, and (2) the exponent. Order matters:

>>> pow(3, 2)�9

>>> pow(2, 3) �8

You have to understand which number is which so that you can use the function correctly

41

42 of 47

Keyword Parameters

  • Keyword parameters are optional
  • They can be passed in any order, but always after the positional parameters
  • Keyword parameters always have a default value
  • Example: Consider the built-in function print(). print() has 0 required parameters — what will be printed — and several optional keyword parameters. One of these is called “file.” If “file” is not specified, text prints to the screen. If a file is specified, it prints to the file

42

43 of 47

Keyword Parameters: Example

>>> name = 'Jackson'�>>> print('Hello', 'there', name, '!\n')�>>> print('Hello', 'there', name, end='!\n')�>>> print('Hello', 'there', name, end='!\n')�>>> print('Hello', 'there', name, sep='~~~~~~', end='!\n')�

43

44 of 47

Outline

  • Statements & Expressions
  • Intro to functions
  • Built-in functions

44

45 of 47

Built-in, imported, and custom functions

  • Some functions are built-in (like print(), input(), and type())
  • Others can be imported from modules (like TKinter functions)
  • You can also make your own

45

46 of 47

Frequently used built-in functions

I/O

  • print()
  • input()

Understanding your data & functions

  • type()
  • dir()
  • help()

For each of the functions listed above...

  • what are the legal arguments? What is the return value? How can you find out?

46

47 of 47

Demo

47