Introduction to Programming: Functions and Sequences Recap
This course covers the basics of programming, focusing on defining and calling functions, handling return values, and manipulating different types of sequences like strings, tuples, lists, and dictionaries. The content includes practical examples and explanations to help you grasp these fundamental concepts effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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 weeks in lab assignment Simple list manipulation
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
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,
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
Functions: define and call def circle_area(radius): return (3.14159 * radius ** 2) This is the definition of a function. This block define what job will circle_area perform, input and output. circle_area(5) print( Now I know the area of the circle ) This is how you call the function: name and parentheses
Functions: define and call def instructions(): 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. ) This is the definition. instructions() This is how you call the function: name and parentheses
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
Functions: Return values See lecture slides: catching a return value
Parameters and Arguments Parameters def circle_area(radius): return (3.14159 * radius ** 2) Arguments circle_area(5) print( Now I know the area of the circle )
Parameters and Arguments Parameters def add(a, b): return (a+b) add(2, 4) Arguments
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
Parameters and Arguments See lecture slides: positional parameters and arguments Keyword argument Default Parameter Values Parameter Type
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.
In Lab Assignments See Assignment 7
Have a nice evening! See you tomorrow~