Overview/Questions Whats the point? Writing functions Calling - - PDF document

overview questions
SMART_READER_LITE
LIVE PREVIEW

Overview/Questions Whats the point? Writing functions Calling - - PDF document

CS101 Lecture 23: The Function of Functions Aaron Stevens 23 March 2009 1 Overview/Questions Whats the point? Writing functions Calling functions Formal parameters Actual parameters Scope rules and name spaces


slide-1
SLIDE 1

1

1

Aaron Stevens

23 March 2009

CS101 Lecture 23: The Function of Functions

2

Overview/Questions

– What’s the point? – Writing functions – Calling functions – Formal parameters – Actual parameters – Scope rules and name spaces – Return values

slide-2
SLIDE 2

2

3

Functions: what’s the point?

We’ve used a lot of functions in our programs, in several forms:

– Built-ins functions, like input()and float() – Library functions, like math.tangent()

Why did we use those? What was in it for us?

4

Function Example

Example: happy birthday program

– Start with simple program with some repeated statements, group into function. – Add: formal parameter to receive name.

slide-3
SLIDE 3

3

5

Functions: Simple Example

6

Functions: what’s the point?

Think of a a function as a subprogram – a program within a program. Function A sequence of statements which are given a name. This sequence can then be invoked by calling the name.

slide-4
SLIDE 4

4

7

Function Declaration Syntax

def function(<formal-parameter-list>): <statement> <statement> – The keyword def indicates the identifier that follows it is a new function name. – The <formal-parameter-list> is a list of 0 or more parameters required to invoke the function. – The parentheses are a required part of the definition, as is the colon after the parentheses.

8

Formal Parameters

Functions take parameters for 2 reasons: – As changeable inputs to the function

  • Example: name in happy birthday program

– Variables in a function are local to that function (scope)

  • Example: name in birthday program
slide-5
SLIDE 5

5

9

Variable Scope

Scope refers to the places in a program where a variable name is visible (allowed to be read). – The variables in a function are local to that function. – To use a variable from outside the function it must be passed/received in as a parameter.

10

Why not use global variables?

Global variables are declared at the top of a file, and are visible in the entire program. Why not use global variables all the time?

– Namespace gets cluttered – Naming conflicts, hard to remember which is which

slide-6
SLIDE 6

6

11

Actual Parameters

A function definition has the form:

def function(<formal-parameter-list>): <statement> <statement>

A function call has the form:

function(<actual-parameter-list>)

At run time, the actual parameters (passed by the caller) are (shallow) copied into the formal parameter list. Thus, the function can use the variables declared in its formal parameter list.

12

Return Values

We think of parameters as inputs to a function. Sometimes we want to call a function and get data back. This data is called a return value. A function returns values by using the return statement:

return <expression>

slide-7
SLIDE 7

7

13

Return Values

Example: calculating area (single return value)

14

Take-Home Points

– Why use functions – Variable Scope – Formal Parameters – Actual Parameters – Return Values

slide-8
SLIDE 8

8

15

Student To Dos

– HW09 due Tuesday 3/24 – QUIZ 4 is on FRIDAY 3/28

  • Covers material in lectures (16-22)
  • Flash concepts, motion tweens, images, audio
  • Programming languages, python introduction
  • Elements of python programs, computing with numbers

– Reading: How to Think Like A Computer Scientist: Learning with Python

Available online at: http://openbookproject.net//thinkCSpy/

  • Ch03 (today)
  • Ch04 (Wednesday/Friday)