Title: Pascal 1 Week 9 Day 1
1Pascal 1 Week 9 Day 1
2Program Song procedure Chorus begin
writeln(Oh, I dont care too much for Army
life!) writeln(Gee Mom, I wanna go back
where the roses grow) writeln(But they
wont let me go home.) end procedure
FirstVerse begin write(They say that I the
Army) writeln( the coffees mighty
fine.) write(Its good for cuts and
bruises,) writeln( and tastes like
iodine.) end procedure SecondVerse begin
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.) end begin Of the
main body FirstVerse Chorus writeln Second
Verse Chorus end.
3Procedures and Functions
- What are they?
- Subprograms Like GOSUBs in BASIC but with
greater power. - If it returns only one value use a function,
otherwise use a procedure
4Why are they needed?
- 1) As your programs grow longer and longer
they become more difficult to read. Procedures
and functions allow you to break your program
into smaller programs. - 2) Allow you to work in groups on a program
and have each member of the group write a section
of code (procedure/function) and you can piece it
together more easily. - 3) Allow for recursion.
5Where are they located?
- Procedures and functions go in between the VAR
declarations and the BEGIN of the main body.
6How does it work?
- It takes three parts to make a subroutine
- The Call Statement
- The Header
- The Code
7The Call Statement
- This calls the subroutine into action by using
the subroutine name and sends information through
the argument list (More on this later.) In the
previous example, all of the call statements were
in the main body. (FirstVerse Chorus) - For the rest of today we will focus on Procedures
and hit functions later.
8Calling Procedures
- ProcedureName(ArgumentList)
- Argument List Contains variables, values and
expressions, separated by commas, that are sent
to the subroutine and variables that save answers
found in the subroutine. (More Later)
9Sample Procedure call statements
Procedure name
Argument List
- FirstVerse
- Field(Money)
- Show(money,Game, Bet, Roll, Win, Newbal)
- Chorus
- Deposit(Savings,SavingsBalance,amount)
- Play(song, 5)
- DoubleEm(total, 28)
10The Header
- This defines the name of the subroutine and the
name and types of the variables sent to the
subroutine (parameter list).
11Procedure Procedurename(ParameterList)
- Procedure This is a reserved word and it tells
the computer you are starting a procedure. - ProcedureName This is an identifier name,
following the same rules as those for variables.
This is the exact name the must be used in the
call statement.
12Procedure 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.
13Sample Headers
- Procedure Chorus
- Procedure FirstVerse
14Quick Review
- Subroutines are like mini programs
- Subroutines have three parts
- Call statement
- Name(Arguments)
- Header
- Procedure Name(Parameterstypes)
- Code
15The Code
- Locally declared variables
- Can include const, type, and var declarations
- The variables only exist while the subroutine is
running - Can use all the constructs
- Can call other subroutines that are declared
above the subroutine
16Adding vocab. to the example
Program Song procedure Chorus begin
writeln(Oh, I dont care too much for Army
life!) writeln(Gee Mom, I wanna go back
where the roses grow) writeln(But they
wont let me go home.) end procedure
FirstVerse begin write(They say that I the
Army) writeln( the coffees mighty
fine.) write(Its good for cuts and
bruises,) writeln( and tastes like
iodine.) end procedure SecondVerse begin
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.) end begin Of the
main body FirstVerse Chorus writeln Second
Verse Chorus end.
Header
Code
Call Statement
17Program Options
- 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) - Write procedures to draw a person doing jumping
jacks. A separate procedure for each position and
cycle through the positions with a loop.