Computer Science Procedures Day 2.

Slides:



Advertisements
Similar presentations
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Advertisements

Functions:Passing Parameters by Value Programming.
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
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.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Programming, an introduction to Pascal
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
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,
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Computer Science 2 Arrays of Records. First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) –Output:
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Understand how to break down a program into procedures
FIGURE 4-10 Function Return Statements
Functions CIS 40 – Introduction to Programming in Python
Program Options: 10 Minutes online
Review Dry Run A Little More Math Fix it Online time
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Computer Science and an introduction to Pascal
FIGURE 4-10 Function Return Statements
Computer Science Procedures
Computer Science II First With files.
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
Pascal Subprogram Procedure Function Build in Function (e.g. Sin(x))
Program Options: 10 Minutes online
Computer Science
FIGURE 4-10 Function Return Statements
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Computer Science Sorting.
Computer Science 2 Arrays of Records.
6.001 SICP Environment model
Computer Science 1 Warm-up: True/False Dry Run
Computer Science
CS 432: Compiler Construction Lecture 11
Computer Science II Second With files.
Computer Science 2 Take out a piece of paper for the following dry runs. Label It Recursive Dry Runs 4/12/2018. It will be turned in when completed
Computer Science Procedures Day 2.
Continue with Life, Magic and Catch -up
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
FIGURE 4-10 Function Return Statements
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.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Computer Science II First With files.
Presentation transcript:

Computer Science Procedures Day 2

Learning Objectives Review procedures Understand parameters and arguments. Practice reading programs that use procedures

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?

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

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

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

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

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

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.

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

Learning Objects Review Procedures Practice reading programs that have procedures Develop a program that uses procedures with parameters.

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.

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

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.

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.

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.

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.