Computer Science Procedures Day 2.

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Advertisements

Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Lecture 16 Pass by value vs. pass by reference (Still) Appearances are often deceiving Aesop, Fables.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Introduction to Pascal The Basics of Program writing.
David Stotts Computer Science Department UNC Chapel Hill.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
Programming, an introduction to Pascal
Assignment statement and Arithmetic operation 1 The major part of data processing.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
Program Options: 10 Minutes online Write a program that will display your home address as it would on an envelope. –First Name Last Name –Address –City,
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Computer Science 2 Arrays of Records. First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) –Output:
Structured Programming
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Understand how to break down a program into procedures
With a different method
Functions CIS 40 – Introduction to Programming in Python
Program Options: 10 Minutes online
Review Dry Run A Little More Math Fix it Online time
Java Methods Making Subprograms.
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Lesson #6 Modular Programming and Functions.
Computer Science and an introduction to Pascal
Structured Programming
Computer Science Procedures
Computer Science II First With files.
Java Methods Making Subprograms.
Review Dry Run Taking Names Online time Math is Good
Computer Science 2 Arrays of Records.
Computer Science 1 Get out your notebook
Continue on the Array of Records program
Computer Science 2 Arrays of Records.
A function with one argument
Computer Science And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and.
Computer Science 2 Getting an unknown # of …. Into an array.
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Program Options: 10 Minutes online
Faculty of Computer Science & Information System
Using Functions
Computer Science Sorting.
Computer Science 2 Arrays of Records.
Computer Science 1 Warm-up: True/False Dry Run
Computer Science
Lesson #6 Modular Programming and Functions.
CS 432: Compiler Construction Lecture 11
Computer Science 1 Online time for Graphics Program Random review
How can you make a guessing game?
Computer Science 2 More Trees.
Computer Science Procedures Day 2.
Computer Science 1 while
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Warm-up: Dry Run DIV / MOD 2nd If Then Program
Computer Science
Warm-up: True/False Dry Run DIV / MOD 2nd If Then Program
Computer Science I: Get out your notes.
Continue on the Valentines program
Computer Science 1 while
Computer Science 1 Get out your notebook
Computer Science 1 Get out your notebook
Dry Run Fix it Write a program
Computer Science And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and.
CPS125.
A bit of Review Review, Dry Run, Program.
Top-Down Design with Functions
Computer Science II First With files.
Presentation transcript:

Computer Science Procedures Day 2

Learning Targets Review Procedures: 20 minutes time to finish procedure 1 program from December Know the parts of a procedure header Be able to recognize a variable and value parameter. Be able to Dry run a program that uses variable and value parameters Be able to write a program that uses variable and value parameters

Adding vocab. to the example Program Song; procedure Chorus; begin writeln(‘Oh, I don’’t care too much for Army life!’); writeln(‘Gee Mom, I wanna go back where the roses grow’); writeln(‘But they won’’t let me go home.’); end; procedure FirstVerse; write(‘They say that I the Army’); writeln(‘ the coffee’’s mighty fine.’); write(‘It’’s good for cuts and bruises,’); writeln(‘ and tastes like iodine.’); procedure SecondVerse; Var loopy:integer; Begin for loopy:= 1 to 3 do write(‘They say that in the Army, ‘); writeln(‘the biscuits are real fine.’); write(‘One rolled off a table, ‘); writeln(‘ and killed a pal of mine.’); Header begin {Of the main body} FirstVerse; Chorus; writeln; SecondVerse; end. Call Statement Local Variable. Code

1) Modify the Army song by using a song of your choice or your creation. It must be rated ‘G.’ 2) Write a program that uses three procedures to draw a person. Procedures DrawHead (A circle would be fine), DrawBodyAndArms (Lines are fine), and DrawLegs (Lines are fine) 3) Write procedures to draw a person doing jumping jacks. A separate procedure for each position and cycle through the positions with a loop. Push: Have them run across the screen. Push: Have a dance team Push: Have them change direction based on user input. Program Options

Review program DryRunCheck; var a, b:integer; {*******************************} procedure first(one, two: integer); three:integer; begin writeln('Hello '); three:= two - one; writeln(one:5, two:5, three:5); end; {******************************} begin // Main Body a:=10; b:=15; writeln(a:5,b:5); first(a,b); end. What is the name of the procedure? Where is the call statement? Where is the header? Where is the code for the procedure? Where is the code for the main body? What are the local variables for the procedure? What are the global variables for the program?

The Header with Parameter Lists Get out your notes: The Header with Parameter Lists procedure Deposit(account:string; var Balance, amount:real); The Header defines the name of the procedure and the name and types of the variables sent to the subroutine (parameter list).

Procedure Procedurename(ParameterList); Parameter List: This lists variables, and their types, for ALL information that gets sent to and returned from the procedure. Note: there is a one-to-one relationship between the parameter list and the argument list. (Variable:type) (variable1, variable2:type;variable3:type); Example: procedure Deposit(account:string; var Balance, amount:real); Corresponding call statement from the main body Deposit(‘Savings’,SavingsBalance,amount); Note the Punctuation

Variable and Value parameters procedure Deposit(account:string; var Balance, amount:real); Variable Parameters (Arrow) Has a var in front of the variable. Can have commas between variable parameters in the header. As the variable changes in the procedure, it changes the global variable sent to it. It is used for getting or changing information in the procedure, adding to a total, etc. Must be sent a variable from the call statement!!! Value Parameters (Box) Those that will not change. Can be sent a variable (x), value (12), or expression (2*b+8).

Recognizing Parameters procedure Instructions(Questn:char; Level:integer); procedure Field(var Money, bet:real); procedure Show(money:real; Game:string; Bet:real; Roll:integer; Win:real; Newbal:real); procedure Deposit(account:string; var Balance, amount:real); Variable Parameters: Can only accept variables Value Parameter: Can accept variables, values or expressions

Adding vocab. to the example Program ProcedureExample; var num1,num2,difference:integer; {************************************} procedure GetTheNumbers(var first, second:integer); Var total:integer; begin writeln(‘Please enter a number’); readln(first); writeln(‘Please enter another number’); readln(second); total:= first+second; Writeln(‘The total = ‘, total); end; {**********************************} begin {Of the main body} GetTheNumbers(num1, num2); difference:=num1- num2; writeln(num1,’ - ‘, num2, ‘ = ‘, difference); readln; end. {Of the main body} Variable Parameters Header Arguments Code Call Statement

Reading a program that has parameters. program confusion; var a, b:integer; {******************************************************} procedure one(d, e:integer); f:integer; begin writeln('One '); f:= d + e; d:= f + 4; writeln(d, e, f); end; {*****************************************************} begin // Main Body a:=5; b:=6; writeln(a,b); one(a,b); end. Reading a program that has parameters. Dry run the following as a class.

Dry run the following program confusion2; var a, b:integer; {******************************************************} procedure one(var d, e:integer); f:integer; begin writeln('One '); f:= d + e; d:= f+4; writeln(d, e, f); end; {*****************************************************} begin // Main Body a:=5; b:=6; writeln(a,b); one(a,b); end. Dry run the following

What do you recall about… Procedures Call statements Headers Variable parameters Value parameters Arguments Local Variables Global Variables

begin {Of the main body} a:=2; b:=3; c:=4; writeln(a,b,c); d(c,b,a); program confusion3; //Dry Run var a,b,c:integer; procedure d(a,b:integer; var e:integer); c:integer; begin c:=a+b; writeln(a,b,c); e:=c+a; end; procedure e(var c:integer; b:integer); a:integer; a:=c+b; begin {Of the main body} a:=2; b:=3; c:=4; writeln(a,b,c); d(c,b,a); e(a,b); end.

begin a:=2; b:=4; c:=6; writeln(a,b,c); one(a,b,c); two(b,a,c); end. program confusion4; //Dry Run var a,b,c:integer; procedure one(var x, a:integer; y:integer); b:integer; begin b:=a+x+y; a:=b-x; writeln(a,b,x,y); x:=b; end; procedure two(x,y:integer; var z:integer); a:integer; a:=x+y; y:=a+y; z:=a+x; writeln(a,x,y,z); begin a:=2; b:=4; c:=6; writeln(a,b,c); one(a,b,c); two(b,a,c); end.

Developing a program that uses procedures with parameters. Bubble Diagram Pascal Task: “Write a procedure that is sent two integer values, then adds them up and shows the answer inside the procedure” Start by creating a Bubble Diagram Up only arrows are Value Parameters Down arrows are Variable Parameters

Developing a program that uses procedures with parameters. “Modify this so that it will add the numbers in the procedure and show the answer in the main body.” Start by creating a Bubble Diagram Up only arrows are Value Parameters Down arrows are Variable Parameters

Take two minutes to Summarize the following in your notes. Procedure Header Call Statement Variable Parameter Value Parameter Bubble Diagram

Program Options Write a program that will have a procedure get 10 numbers and calculate the total and average of the 10 numbers. Then show the total and average in the main body. Write a program that will get the number of stick figures you would like to display in the main body. Show that many figures from the procedure Write a program that will input the real and imaginary parts of two complex numbers in the main body Add the numbers in a procedure Show the total in the main body Example: Input: 5 + 2i, and 2 + 3i it would return 7 + 5i Note you will need a separate variable for the real and imaginary parts of the number Push: Create a procedure for multiplying complex numbers. Push: A procedure for raising a complex number to a positive integer power.