Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Procedures Day 2.

Similar presentations


Presentation on theme: "Computer Science Procedures Day 2."— Presentation transcript:

1 Computer Science Procedures Day 2

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

3 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

4 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

5 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?

6 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).

7 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

8 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).

9 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

10 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

11 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.

12 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

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

14 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.

15 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.

16 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

17 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

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

19 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.


Download ppt "Computer Science Procedures Day 2."

Similar presentations


Ads by Google