Example 1.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
1 C++ string Class Chapter 6. 2 Agenda String Basics (cin, getline )  string operations mixed I/O using >> & getline() Table Output using setw() Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Introduction to Python
ColdFusion Variables CF uses variables to store data in memory. There are many different types of variables; each has its own use. To use a variable,
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
DECOMPOSITION. KEY TERMS  Structured programming  Functionality  Structure Charts  Stepwise refinement  Modularity.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.
More Functions with Return Values CS303E: Elements of Computers and Programming.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
University of Malta CSA2090: Lecture 4 © Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.
Functions Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
ITEC 109 Lecture 14/15 Strings + Functions. Review Strings –What can we do? –Why?
Python Let’s get started!.
Values, Types, and Variables. Values Data Information Numbers Text Pretty much anything.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
Expressions Methods if else Statements Loops Potpourri.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
CS0004: Introduction to Programming
Test 2 Review Outline.
Course A201: Introduction to Programming
Functions A function is a block of code with a name. function functionName() { code to be executed; }
Examples of Classes & Objects
Python Let’s get started!.
Chapter 10: Void Functions
Command Line Arguments
Case Statements and Functions
Week 8 - Friday CS 121.
#include "std_lib_facilities
Using local variable without initialization is an error.
REBOL Writing Functions.
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Teaching London Computing
An Introduction to Java – Part II
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Classes & Objects: Examples
Variables and Their scope
Passing Parameters by value
Format String.
CS 1111 Introduction to Programming Fall 2018
Functions as everyday items
Nate Brunelle Today: Functions again, Scope
Chapter 7 The Java Array Object © Rick Mercer.
Class Examples.
Function “Inputs and Outputs”
STORE MANAGER RESPONSIBILITIES.
functions: argument, return value
Nate Brunelle Today: Functions
Introduction to Computer Science
Questions and Statements
Chapter 6 Modular Programming chap6.
Nate Brunelle Today: Functions
Methods/Functions.
Nate Brunelle Today: Functions again, Scope
Questions and Statements
Functions, Procedures, and Abstraction
© Sangeeta M Chauhan, Gwalior
Programming Techniques
Chapter 4 Test Review First day
Presentation transcript:

Example 1

def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a )

singthrice( x ) x def singthrice( x ): sing the value in x three times, loudly 2 def hop( y ): hop y times main() def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a )

def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) hop( y ) y 1

singthrice( x ) x def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times 154 main() def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a )

def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) -1

singthrice( x ) x def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) -1

def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) -1 hop( y ) y 2

def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) 1

def singthrice( x ): sing the value in x three times, loudly def hop( y ): hop y times main() a def main( ): singthrice( 2 ) hop( 1 ) singthrice( 154 ) a=-1 singthrice( a ) # -1 -1 -1 #not a a a hop( 2 ) a = 1 hop( a ) 1 hop( y ) y

Notes on Example 1 literals as arguments: 2, 154 variables as arguments: a pass by value: argument to parameter main's local variables: a singThrice's local variables: x hop's local variables: y scope of local variables: only understood in function in which they are defined.

Example 2

def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S')

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c " T" def writeThisLetter( c ): write the value of c main() def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') output

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c " T" def writeThisLetter( c ): write the value of c return "U" main() def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') output

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c " T" def writeThisLetter( c ): write the value of c return "U" main() x def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" output

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c "R" main() x def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" output

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c "R" main() x def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" return "S" output

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c "R" main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" return "S" "S" output

def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" "S" output GO SU!

def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" "S" output GO SU! USA

def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" writeThisLetter(c) c "S" "B" output GO SU! USA

def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" writeThisLetter(c) c "S" output GO SU! USA "U"

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y "G" def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" "S" output GO SU! USA

tellNextLetter(c) c def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y "G" def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+"!") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" return "H" "S" output GO SU! USA

def tellNextLettet( c ): tell main the letter after the value of c def writeThisLetter( c ): write the value of c main() x y def main( ): x=tellNextLetter('T') y=tellNextLetter('R') print("GO "+y+x+" !") print(x+y+"A") writeThisLetter('B') writeThisLetter('U') tellNextLetter('G') writeThisLetter('S') "U" writeThisLetter(c) c "S" output GO SU! USA "S"

Notes on Example 2 string literals as arguments : 'T' or "T" return values may be stored in a variable and used: x=tellNextLetter('T') or return values may be ignored: tellNextLetter('G') scope of parameters