λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

University of California, Berkeley/ElectricalEngineering & ComputerSciences

Functions

Lecture 2 – Names & Functions

by. Chris Allsman

 

Announcements

-      Hw0 released, due Friday

: a survey but get it done early so you don’t forget

-      Tentative dates for assignments:

: Hw1 released tomorrow, due Tuesday

: Proj1 released Thursday, due July 8th

-      Discussion/OH starts today

-      Lab starts tomorrow or Thursday

 

Policies, Part 2

Communication

You should use piazza or reach out to your section TA for 99% of questions/issues

You will be expected to keep up with piazza announcements, but you may wish to turn off email notifications

Mental Health/DSP Accommodations

The instructors have access to DSP letters submitted, and will reach out closer to the exams for accomodations

 

Program Structure

Review – Expressions

Primitive Expressions : numbers, strings, names

Arithmetic Expressions : 1 + 2   15 // 3

Call Expressions : max ( add ( 2, 3 ) )

Review – Evaluating Call Expressions

1. Evaluate

 a. Evaluate the operator subexpression

 b. Evaluate each operand subexpression

2. Apply

 

Values

 : programs manipulate values

values represent different types of data

Integers / Floats / Strings / Booleans

Expressions & Values

 : Expressions evaluate to values in one or more steps

Expression : “hello!”, 7 / 2, add(1, max(2,3))

Value : “hello!”, 3.5, 4

we think of writing w/ expression but computer uses values.

python : X distinctions between strings and numbers

Names

python codes ex: from math import pi

Values can be assigned to names to make referring to them easier.

A name can only be bound to a single value.

One way to introduce a new name in a program is with an assignment statement.

Statements affect the program, but do not evaluate to values.

Check Your understanding

f = min

f = max

g, h = min, max

max = g

max ( f ( 2, g ( h ( 1, 5 ), 3 ) ), 4 )

ans. h(1,5) = 5, g(5,3) = 3, f(2,3) = 3, max(3,4) = 3 à 3

Visualizing Assignment

Names are bound to values in an environment

To execute an assignment statement

Functions

( functions are another type of value )

Functions allow us to abstract away entire expression and sequences of computation.

They take in some input (know as their arguments) and transform it into an output (the return value)

We can create functions using def statements. Their input is given in a function call, and their output is given by a return statement.

Defining Functions

Function signature indicates name and number of arguments.

def <name> (<parameters>):

           return <return expression>

Function body defines the computation performed when the function is applied.

Execution rule for def Statements

1. create a function with signature <name>(<parameters>)

2. set the body of that function to be everything indented after the first line

3. bind <name> to that function in the current frame

Function in Environment Diagrams

Global frame

           mul : func mul

           square : func square(x)

from operator import mul

def square (x):

           return mul(x,x)

y = square(-2)

def statements are a type of assignment that bind names to function values

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (for now)

1. Create a new environment frame

2. Bind the function’s parameters to its arguments in that frame

3. Execute the body of the function in the new environment

python codes ex.

def square(x):

           return x * x

square(-2)

f1 : square

           x = -2

           return value = 4

 

'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Control  (0) 2019.06.27
DISC  (0) 2019.06.27
Intro  (0) 2019.06.25
Week 1  (0) 2019.06.25
The Structure and Interpretation of Computer Programs  (0) 2019.06.25