1 / 17

Course A201: Introduction to Programming

Course A201: Introduction to Programming. 11/04/2010. Outline for this week. Last week’s lab assignment and recap Function General structure How to define a function; call a function Return values Parameters and Arguments. Last week’s in lab assignment. Simple list manipulation. Recap.

spillman
Download Presentation

Course A201: Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Course A201:Introduction to Programming 11/04/2010

  2. Outline for this week • Last week’s lab assignment and recap • Function • General structure • How to define a function; call a function • Return values • Parameters and Arguments

  3. Last week’s in lab assignment • Simple list manipulation

  4. Recap • Sequences we’ve learned so far: • String: ‘hello’, ‘goodbye’ • Tuple: (‘hello’, ‘begin with h’), (‘goodbye’, ‘end with e’) • List: [‘hello’, ‘goodbye’, 1, 0.9] • Dictionary: {1:‘hello’, 2:‘goodbye’} • Useful methods belongs to each type

  5. Recall Function [functions] • input1, input2, input3, … • You give me some inputs I am a function. I will perform a certain job. • I give you some outputs back, explicitly or implicitly • output1, output2, output3, …

  6. Functions • Built-in function len(): • Input: a sequence • output: an integer • Job: count how many items are there in this sequence • print(): • Input: any type of value • Output: None • Job: print out the inputs

  7. Functions: define and call defcircle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) • This is the definition of a function. This block define what job will circle_area perform, input and output. • This is how you call the function: name and parentheses

  8. Functions: define and call definstructions(): print(“““Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe. This will be a showdown between your human brain and my silicon processor.”””) instructions() • This is the definition. • This is how you call the function: name and parentheses

  9. Functions: Return values • So, from the above two examples: • Function circle_area() does return a value, which is the area of a circle • Function instructions() does NOT return a value, and by default the return value will be None • The keyword return

  10. Functions: Return values • See lecture slides: catching a return value

  11. Parameters and Arguments • Parameters defcircle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) • Arguments

  12. Parameters and Arguments • Parameters defadd(a, b): return (a+b) add(2, 4) • Arguments

  13. Parameters and Arguments • Parameters are essentially variable names inside the parentheses of a function header: • add(a,b), a and b are parameters of the function add • You can have 0, 1 or many parameters • The function receives values/arguments through its parameters e.g. add(1,2) • The code in the function add now treats a and b as variables with values 1 and 2

  14. Parameters and Arguments • See lecture slides: • positional parameters and arguments • Keyword argument • Default Parameter Values • Parameter Type

  15. Default Parameters Values • Once you assign a default value to a parameter in the list, you have to assign default values to all the parameters listed after it. So, this function header is perfectly fine: def monkey_around(bananas = 100, barrel_of = "yes"): But this isn't: def monkey_around(bananas = 100, barrel_of): The above header will generate an ERROR.

  16. In Lab Assignments • See Assignment 7

  17. Have a nice evening! • See you tomorrow~

More Related