Computer Science Procedures 1-11-2018
Learning Objectives Review procedures Practice reading programs that have procedures. Going through the Dry Runs from the Worksheet Practice writing a code using procedures with Bubble Diagrams.
Matching. Number you paper 1 - 8 The part of code that defines the procedure name and the parameters. The information in the call statement that is sent to the procedure. A variable defined inside a procedure. A sub program. Variables defined in the main body The line of code in the main body that will start the procedure. A parameter that can change the value of its corresponding argument. A parameter that cannot change the value of its corresponding argument. 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); Write the Global Variable names at the bottom For each procedure, write the parameters and local variables. Write boxes under the value parameters, and arrows under the variable parameters. Dry run the program program confusion1; //Dry Run var a,b,c:integer; procedure first(a,b:integer; var e:integer); c:integer; begin c:=a+b; writeln(a,b,c); e:=c+a; end; procedure second(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); first(c,b,a); second(a,b); end.
procedure d(var a,b:integer; f:integer); c:integer; begin c:=a+b; program confusion2; var a,b,c:integer; procedure d(var a,b:integer; f:integer); c:integer; begin c:=a+b; writeln(a,b,c, f); f:=c+a; end; procedure e(c:integer; var b:integer); a:integer; a:=c+b; writeln(a,b,c); Write the Global Variable names at the bottom For each procedure, write the parameters and local variables. Write boxes under the value parameters, and arrows under the variable parameters. Dry run the program begin {Of the main body} a:=3; b:=21; c:=16; writeln(a,b,c); d(c,b,a); e(a,b); end.
procedure a(var b,c:integer); var d:integer; begin d:=b+c; c:=b + 10; Program Confusion3; procedure a(var b,c:integer); var d:integer; begin d:=b+c; c:=b + 10; writeln(b,c,d); end; procedure e(var f:integer; g:integer); f:= 2*g; writeln(f,g); a(g,f); Write the Global Variable names at the bottom For each procedure, write the parameters and local variables. Write boxes under the value parameters, and arrows under the variable parameters. Dry run the program var x,y,z:integer; begin x:=4; y:=6; z:=8; writeln(x,y,z); a(x,y); e(y,z); end.
Programming with Procedures: Bubble Diagrams Input: In the main body. Two integer values Process: Write a procedure that will be sent two integer scores and return in the scores in order. So if they are already in order, or have the same value they are not switched, if they are not in the same order, then they are switched. Output: In the main body. The numbers in order. 2) Input: In the main body, two positive integers Process/Output: Write a procedure that sent two integers and shows (writeln) the result of the first integer raised to the second integer’s power. Example: Sent, 2 and 3 it will show 2^3 = 8 3) Write a procedure that is sent nothing, but the procedure will get an unknown number of positive scores and return the average and the range of scores entered. (Note, return and average and range of -1 if no scores are entered in the procedure.) 10, 20, 12, 15, 18, 5, 4 Will return 84/7 = 12 for the average And 20 – 4 = 16 for the range