Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables.

Slides:



Advertisements
Similar presentations
Pascal Syntax. What you have learnt so far writeln() and write(); readln() and read(); variables (integers, strings, character) manipulation of variables.
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
No calculators or notes can be used on this quiz.
Introduction to computers & programming Part Deux Abandon all hope, ye who enter here Dante, The Divine Comedy.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 2.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 2, Lecture 2.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Basic Input/Output and Variables Ethan Cerami New York
Math 010 online work that was due today at the start of class:
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Agenda Review Compiling Review Data Types Integer Division Composition C++ Mathematical Functions User Input Reading: , 8.11 Homework #3.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Structured Programming Defn: This is an approach to program development that produces programs of high quality that are easy to test, debug, modify and.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
CS0004: Introduction to Programming Subprocedures and Modular Design.
Lecture 13 Midterm overview When you know a thing, to hold that you know it, and when you do not know a thing, to allow that you do not know it: this is.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
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.
Basic & Advanced Reporting in TIMSNT ** Part Two **
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
For loops in programming Assumes you have seen assignment statements and print statements.
David Stotts Computer Science Department UNC Chapel Hill.
Python Functions.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft RAM Allocation Chapter 3.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
JavaScript, Fourth Edition
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
Math 010 online work that was due today at the start of class: Section 1.3A Online Homework.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Gator Engineering Google Code Jam 2015 Copyright © 2008 W. W. Norton & Company. All rights reserved. 1.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 7: User-Defined Functions II
Suppose we want to print out the word MISSISSIPPI in big letters.
Principles of programming languages 4: Parameter passing, Scope rules
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
One-Dimensional Array Introduction Lesson xx
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Getting Started With Coding
Presentation transcript:

Lecture 15 Variable passing Appearances are often deceiving Aesop, Fables

Last lecture review Procedure is a building block Program consists of several of procedures Procedure looks like a small program Procedures are declared using keyword ‘PROCEDURE’ Procedure has its own scope, that is variables declared in the procedure are accessible within procedure only Global variables declared before procedure are accessible in this procedure Global variables are always accessible in the main program

Optional program from last time Write program which has procedure square. Procedure Square prints 3x3 square using ‘*’ character. The program should read in the number of squares to be printed

Optional program from last time program SquareViaProcedure; procedure Square; var iLoopCount : integer; begin for iLoopCount := 1 to 3 do begin writeln('***'); end; end; { of procedure Square } { Main program } var iNumSquares, iSquareCount : integer; cDummy : char; begin write( 'Please enter the number of squares: ' ); readln( iNumSquares ); for iSquareCount := 1 to iNumSquares do begin writeln; Square; writeln; end; writeln( 'Press enter to exit...' ); read( cDummy ); end.

What’s wrong with this? procedure FilledLine; begin writeln( ‘********’ ); end;

But what if we want to print longer or shorter lines????? We need some way to specify that length of line is variable. Wait a minute, you said variable?

Parameters to the rescue procedure FilledLine( iLineLength : integer ) var iCount : integer; begin for iCount := 1 to iLineLength do begin write( ‘*’ ); end; writeln; end; Procedure parameter is like a regular variable. It is passed into procedure and it has its own box.

Fine but how do we call this procedure? Like this: FilledLine( 10 ) ---> ********** FilledLine( 5 ) ---> ***** FilledLine( 0 ) --->

Similarly ShallowLine procedure ShallowLine( iLineLength : integer ) begin writeln( ‘*’,’*’:(iLineLength - 1 )); end; ShallowLine( 10 ) ---> * * ShallowLine( 5 ) ---> * * ShallowLine( 0 ) --->

Putting it together Program ProcsWithParameters;... procedure DoSomething( iMyInteger : integer ); begin … end; { Main program } begin … DoSomething( 123 );... DoSomething( -567 ); … DoSomething( ); end. Compiler error Types mismatch!

Big step toward hw 4 The ‘Boo’ program It is a program, which prints B O

Top-down design Where do I start? Ok, main should probably call some procedure(s), right? Lets see. I am thinking in pseudo code print B print O With main program Sure. Think about what the program should do. Very good. So which procedures do you think we would need?

Top-down design That’s easy letterB; letterO; Sure. Now we need to refine what every procedure does. BTW I think its really cool that letterO is a procedure, don’t you? (we can just call it twice to print 2 letter ‘O’ Great, so the main would look like this: { main } begin letterB; letterO; end.

Top-down design Hmm… Before? You mean like for-loops and built-in functions? Yeah, it’s way cool. Okay, lets work on letterB procedure. Remember to use things that we’ve learned before. Yes. Definitely those. Also use procedures that we created before.

Top-down design Oh, I see, you mean like FilledLine and ShallowLine? Now I understand. This is very cool, I can just think in terms of procedures that we wrote before. I don’t need to bother with how they work! Exactly! They are our building blocks. Instead of starting from scratch just think of creating letter ‘B’ using these two procedure.

Letter ‘B’ under the microscope FilledLine( 10 ) ShallowLine( 11 )

I got it! Procedure LetterB; begin FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); end; How about this? Flawless. I am proud of you kid!

Now procedure to print ‘O’ Procedure LetterO; begin FilledLine( 10 ); ShallowLine( 10 ); FilledLine( 10 ); end; Note that we called ShallowLine with 10 here!

Now the whole thing! procedure ClearScreen; VAR iCount : integer; begin for iCount := 1 to 50 do writeln; end; { Clear screen } procedure FilledLine( iLength : integer ); VAR iCount : integer; begin for iCount := 1 to iLength do begin write( '*' ); end; writeln; end; { of procedure FilledLine } procedure ShallowLine( iLength : integer ); begin writeln('*','*':(iLength - 1)); end; { of procedure FilledLine } procedure LetterB; begin FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); ShallowLine( 11 ); FilledLine( 10 ); end; { end of procedure LetterB } procedure LetterO; begin FilledLine( 11 ); ShallowLine( 11 ); FilledLine( 11 ); end; { of procedure LetterO } { Main program } begin ClearScreen; LetterB; LetterO; end.

Now what? Am I the master of procedures now? You are almost there. There is one more really nasty, but powerful concept

What about the value of a variable passed into procedure? Suppose we call a procedure with a variable and procedure assigns some new value to this variable. What happens? Procedure Test( iSomeInteger : integer ); begin … iSomeInteger := 10; … end; var iGlobalInteger : integer; begin … iGlobalInteger = 9; Test( iGlobalInteger ); end.

Optional problem for next time Write a program which reads the number of students’ homework and then loops, prompting user for next grade. Once grade is entered, program outputs corresponding number of stars using FilledLine procedure. This program is simpler than the one discussed in 6.3

Homework Homework 4 is due Monday, November 9 Prepare for a little quiz on

Programs from this lecture Square BOO Pass by value