overview questions
play

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


  1. CS101 Lecture 23: The Function of Functions Aaron Stevens 23 March 2009 1 Overview/Questions – What’s the point? – Writing functions – Calling functions – Formal parameters – Actual parameters – Scope rules and name spaces – Return values 2 1

  2. 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? 3 Function Example Example: happy birthday program – Start with simple program with some repeated statements, group into function. – Add: formal parameter to receive name. 4 2

  3. Functions: Simple Example 5 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. 6 3

  4. 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. 7 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 8 4

  5. 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. 9 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 10 5

  6. 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. 11 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> 12 6

  7. Return Values Example: calculating area (single return value) 13 Take-Home Points – Why use functions – Variable Scope – Formal Parameters – Actual Parameters – Return Values 14 7

  8. 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) 15 8

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend