CSE 115 Introduction to Computer Science I Road map Review - - PowerPoint PPT Presentation

cse 115
SMART_READER_LITE
LIVE PREVIEW

CSE 115 Introduction to Computer Science I Road map Review - - PowerPoint PPT Presentation

CSE 115 Introduction to Computer Science I Road map Review Defining functions Calling functions Function calls as expressions Functions Calling a function The expressions 2*4 and 3-1 are 'arguments' of the function pow( 2*4 , 3-1 )


slide-1
SLIDE 1

CSE 115

Introduction to Computer Science I

slide-2
SLIDE 2

Road map

▶︎ Review ◀ Defining functions Calling functions Function calls as expressions

slide-3
SLIDE 3

Functions

pow( 2*4 , 3-1 ) computes 82, or 64

Calling a function

the name of the function is 'pow' The expressions 2*4 and 3-1 are 'arguments' of the function

slide-4
SLIDE 4

Road map

Review ▶︎ Defining functions ◀ Calling functions Function calls as expressions

slide-5
SLIDE 5

Defining functions

We are not restricted to using built-in functions. We can define our own!

slide-6
SLIDE 6

Defining functions

Here's an example of a function definition: def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-7
SLIDE 7

Defining functions

The parts are: 'def' is a keyword def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-8
SLIDE 8

Defining functions

'averageOfThree' is a name: you get to choose this def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-9
SLIDE 9

Defining functions

'x', 'y', and 'z' are parameters. A parameter is a variable whose value is assigned (from the corresponding argument) when the function is called. def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-10
SLIDE 10

Defining functions

'(x, y, z)' is a parameter list. The parentheses and commas are 'delimiters' (punctuation) def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-11
SLIDE 11

Defining functions

: is also a delimiter def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-12
SLIDE 12

Defining functions

: is also a delimiter It separates the header of the definition from its body def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-13
SLIDE 13

Defining functions

The body is a block (sequence) of statements. These statements are carried out when the function is called. def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

Python refers to a block of statements as a "suite".

slide-14
SLIDE 14

The return statement

The return statement consists of the keyword 'return' followed by an expression. def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

The value of the expression average is returned as the value of the function

slide-15
SLIDE 15

The return statement

The value of the expression becomes the value of the function call. def averageOfThree(x, y, z): average = (x + y + z) / 3 return average

slide-16
SLIDE 16

Road map

Review Defining functions ▶︎ Calling functions ◀ Function calls as expressions

slide-17
SLIDE 17

Calling functions

What happens when this function call takes place? averageOfThree( 3, 7, 4 )

slide-18
SLIDE 18

Calling functions

Step 1: argument values are assigned to parameters averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

slide-19
SLIDE 19

Calling functions

Step 1: argument values are assigned to parameters averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

name value x y z

We keep track of the values of variables in a table. Such a table is called an environment.

3 7 4

slide-20
SLIDE 20

Calling functions

Step 2: statements in body are executed in sequence averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

The body of a function is a sequence of statements.

name value x y z 3 7 4

slide-21
SLIDE 21

Calling functions

Step 2a: the assignment statement is done first averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

An assignment statement has the form: variable = expression

name value x y z 3 7 4

slide-22
SLIDE 22

Calling functions

Step 2a: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

To evaluate the expression, look up the values of all the variables in the environment table.

name value x y z 3 7 4

slide-23
SLIDE 23

Calling functions

Step 2a: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

(x + y + z) / 3 =

name value x y z 3 7 4

slide-24
SLIDE 24

Calling functions

Step 2a: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

(x + y + z) / 3 = (3 + 7 + 4) / 3 =

name value x y z 3 7 4

slide-25
SLIDE 25

Calling functions

Step 2a: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

(x + y + z) / 3 = (3 + 7 + 4) / 3 = 14 / 3 = 4.666666666666667

name value x y z 3 7 4

slide-26
SLIDE 26

Calling functions

Step 2a: the variable is assigned the value of the expression averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

(x + y + z) / 3 = (3 + 7 + 4) / 3 = 14 / 3 = 4.666666666666667

name value x y z 3 7 4

slide-27
SLIDE 27

Calling functions

Step 2a: the variable is assigned the value of the expression averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

(x + y + z) / 3 = (3 + 7 + 4) / 3 = 14 / 3 = 4.666666666666667

name value x y z average 3 7 4 4.666666666666667

slide-28
SLIDE 28

Calling functions

Step 2b: the return statement is executed next averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

A return statement has the form return expression

name value x y z average 3 7 4 4.666666666666667

slide-29
SLIDE 29

Calling functions

Step 2b: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

The expression is simple: the value of a variable expression is simply the value associated with the variable in the current environment table.

name value x y z average 3 7 4 4.666666666666667

slide-30
SLIDE 30

Calling functions

Step 2b: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

average =

name value x y z average 3 7 4 4.666666666666667

slide-31
SLIDE 31

Calling functions

Step 2b: the expression is evaluated averageOfThree( 3, 7, 4 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

average = 4.666666666666667

name value x y z average 3 7 4 4.666666666666667

slide-32
SLIDE 32

Road map

Review Defining functions Calling functions ▶︎ Function calls as expressions ◀

slide-33
SLIDE 33

Calling functions

Step 3: the function call is an expression… averageOfThree( 3, 7, 4 )

slide-34
SLIDE 34

Calling functions

Step 3: …whose value is the value returned by the function averageOfThree( 3, 7, 4 ) =

4.666666666666667

slide-35
SLIDE 35

Calling functions

Another example: the value of averageOfThree( 5, 2, 8 )

slide-36
SLIDE 36

Calling functions

Another example: the value of averageOfThree( 5, 2, 8 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

name value x y z 5 2 8

slide-37
SLIDE 37

Calling functions

Another example: the value of averageOfThree( 5, 2, 8 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

(x + y + z) / 3 = (5 + 2 + 8) / 3 = 15 / 3 = 5.0

name value x y z 5 2 8

slide-38
SLIDE 38

Calling functions

Another example: the value of averageOfThree( 5, 2, 8 ) def averageOfThree( x, y, z ): average = (x + y + z) / 3 return average

The value returned is 5.0

name value x y z average 5 2 8 5.0

slide-39
SLIDE 39

Calling functions

Another example: the value of averageOfThree( 5, 2, 8 ) is 5.0