Download presentation
Presentation is loading. Please wait.
1
Computer Science Procedures Day 2
2
Learning Objectives Review procedures
Understand parameters and arguments. Practice reading programs that use procedures
3
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 arguments sent to the procedure? What are the local variables for the procedure? What are the global variables for the program? What does this program print out?
4
The Header with Parameter Lists
procedure Deposit(account:string; var Balance, amount:real); This defines the name of the procedure and the name and types of the variables sent to the subroutine (parameter list).
5
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 Deposit(‘Savings’,SavingsBalance,amount); Punctuation
6
Two kinds of parameters
procedure Deposit(account:string; var Balance, amount:real); Variable Parameters (Arrow) Have the var in front of the variable): Can change in the procedure. It is used for getting 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).
7
Sample Headers procedure Field(var Money, bet:real);
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
8
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
9
Dry run the following as a class.
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. Dry run the following as a class.
10
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
11
What do you recall about…
Procedures Call statements Headers Variable parameters Value parameters Arguments Local Variables Global Variables
12
Learning Objects Review Procedures
Practice reading programs that have procedures Develop a program that uses procedures with parameters.
13
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.
14
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.
15
What do you recall about…
Procedures Call statements Headers Variable parameters Value parameters Arguments Local Variables Global Variables
16
begin a:=3; b:=2; c:=7; writeln(a,b,c); one(a,b,c); two(b,a,c); end.
program confusion5; //Dry run var a,b,c:integer; procedure one(x, a:integer; var b:integer); begin b:=a+x; a:=b-x; writeln(a,b,x); 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:=3; b:=2; c:=7; writeln(a,b,c); one(a,b,c); two(b,a,c); end.
17
Your turn Bubble Diagram Write a procedure that is sent two integer values, then adds them up and shows the answer (inside the procedure) Modify this so that it will add the numbers in the procedure and show the answer in the main body. Modify this so that the procedure will calculate the sum (+), difference (-) and product (*) and show the answers in the main body.
18
begin a:=3; b:=2; c:=7; writeln(a,b,c); one(a,b,c); two(b,a,c); end.
program confusion6; //Dry Run var a,b,c:integer; procedure one(var x:integer; a:integer; var b:integer); begin b:=a+x; a:=b-x; writeln(a,b,x); x:=b; end; procedure two(var x:integer;y:integer; var z:integer); a:integer; a:=x+y; y:=a+y; z:=a+x; writeln(a,x,y,z); begin a:=3; b:=2; c:=7; writeln(a,b,c); one(a,b,c); two(b,a,c); end.
19
Program Options 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 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 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.