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

Slides:



Advertisements
Similar presentations
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Advertisements

Scope and Lifespan of Identifiers. Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s.
Guide to Programming with Python
16-May-15 Sudden Python Drinking from the Fire Hose.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
More Functions CS303E: Elements of Computers and Programming.
Introduction to Python
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Functions and subroutines – Computer and Programming.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Course A201: Introduction to Programming 11/04/2010.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
General Computer Science for Engineers CISC 106 Lecture 04 Dr. John Cavazos Computer and Information Sciences 09/10/2010.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,
Python Let’s get started!.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Today’s lecture Review chapter 5 go over exercises.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
1 Introduction to Object Oriented Programming Chapter 10.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Course A201: Introduction to Programming 09/09/2010.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Positional Parameters and Global Variables
A variable is a name for a value stored in memory.
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Topics Introduction to Functions Defining and Calling a Void Function
Course A201: Introduction to Programming
Introduction to Programming
Python Let’s get started!.
CS 1110 Introduction to Programming Spring 2017
Programming Fundamentals Lecture #7 Functions
Introduction to Functions
“If you can’t write it down in English, you can’t code it.”
Hello World! Syntax.
5. Functions Rocky K. C. Chang 30 October 2018
Topics Introduction to Functions Defining and Calling a Void Function
Elements of a Python Program
Topics Introduction to Functions Defining and Calling a Function
CS 1111 Introduction to Programming Spring 2019
15-110: Principles of Computing
Starter Activities GCSE Python.
def-ining a function A function as an execution control structure
Parameters and Arguments
Presentation transcript:

Course A201: Introduction to Programming 11/11/2010

Outline for this week Assignment 6 Recap for function Variable scope: global variable vs local variable Recursive function Assignment 8 – Divide into two teams, come up with pseudo code – then choose a partner

Recap Difference between return and print Ex: circle_area.py def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”)

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print (“Now I know the area of the circle”) I’m IDLE for a while…

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print (“Now I know the area of the circle”) Below is my Memory This region shows what I know before I starts to execute the program This region shows what I know during I’m executing the program

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius I know function circle_area ! Start to execute. f- means function, p- means parameter

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius Start to execute. f- means function, p- means parameter (in f-circle_area) radius = 5 Program tells me that now the parameter radius is 5

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Go to function circle_area(radius) f-circle_area with p-radius (in f-circle_area) radius = 5 This region of memory is only for function circle_area

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Go to function circle_area(radius) f-circle_area with p-radius (in f-circle_area) radius = 5 It will be erased when I finish executing the function

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Go to function circle_area(radius) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Go to function circle_area(radius) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 return value =

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Jump out of function circle_area f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value=

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Result stays in the memory of IDLE, but nothing will be shown on the screen f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value= Nothing on screen

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Finish executing the function, goes to the next line f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value= Execute print Now I know the area of the circle

Recap def circle_area(radius): return ( * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) IDLE’s memory Finish executing the function, goes to the next line f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value= Execute print Now I know the area of the circle The result is not stored in any variable, so it is lost!

Recap Therefore, the correct way to call a function with a return statement is: result = circle_area(5) print(“Now I know the area of the circle:”, result) result = result * 2 print(“And also the area of 2 such circles:”, result)

Let’s change to print Difference between return and print Ex: circle_area.py def circle_area(radius): print( * radius ** 2) circle_area(5)

Recap def circle_area(radius): print( * radius ** 2) circle_area(5) IDLE’s memory Go to function circle_area(radius) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5

Recap def circle_area(radius): print( * radius ** 2) circle_area(5) I am asked to print a value Screen shows: f-circle_area with p-radius (in f-circle_area) radius = 5 radius =

Recap def circle_area(radius): print( * radius ** 2) circle_area(5) IDLE’s memory Screen shows: f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 return value = None

Recap def circle_area(radius): print( * radius ** 2) circle_area(5) IDLE’s memory Jump out of function circle_area, no return value f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value is None

Recap def circle_area(radius): print( * radius ** 2) circle_area(5) IDLE’s memory Jump out of function circle_area, no return value f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value is None

Notice What shows on the screen does not equal with what IDLE knows/keeps in its memory When you write all codes in a file and execute it by pressing F5, IDLE will not show what it knows onto the screen unless you tell it to, using print

Recap [parameter] – A special kind of variable – In the parentheses of function definition, separated by commas ex: def circle_area(radius): def print_heading(title):

Recap [argument] – The values in the parentheses in function calls – Each value then is assigned to the corresponding parameter ex: def circle_area(radius): …… print(circle_area(5)) This is a function call

Recap [argument] – The values in the parentheses in function calls – Each value then is assigned to the corresponding parameter ex: def circle_area(radius): …… print(circle_area(5)) “5” is the argument

Recap [argument] – The values in the parentheses in function calls – Each value then is assigned to the corresponding parameter ex: def circle_area(radius): …… print(circle_area(5)) Then radius is assigned with value 5 in the function of cricle_area

Recap [argument] – The values in the parentheses in function calls – Each value then is assigned to the corresponding parameter ex: def circle_area(r1, r2, r3): …… print(circle_area(5, 6, 7)) In the same way: r1 is assigned 5 r2 is assigned 6 r3 is assigned 7

Recap [default parameter values] def birthday1(name= “Jackson”, age=1): print(“Happy birthday,”, name, “!”, “ I hear you're”, age, “today.\n”) >>> birthday1(‘Mary’, 2) Happy birthday, Mary ! I hear you are 2 today. >>> birthday1() Happy birthday, Jackson ! I hear you are 1 today.

Variable scope See lecture slides: Global variable and Local variable Let’s see an example

Recursive Function See lecture slides: Recursive Function