Download presentation
Presentation is loading. Please wait.
Published byBrice Maxwell Modified over 6 years ago
1
Understand how to break down a program into procedures
CS 1 Procedures Day 1 Understand how to break down a program into procedures
2
Learning Objectives Be able to read a program that uses procedures.
Incorporating note taking strategies to help understand the parts of a procedure Header Code Call Statement Be able to write a program that uses procedures.
3
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; 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.’); begin {Of the main body} FirstVerse; Chorus; writeln; SecondVerse; end.
4
Notes/Vocabulary Procedures Questions, Subtitles, Headings, Etc.
First & Last Name Class Title Period Date Procedures Questions, Subtitles, Headings, Etc. Notes/Vocabulary Procedure Function Call statement Argument Header Parameter Code for a procedure … 2 1/2”
5
Procedures and Functions
What are they? Little programs If it returns only one value use a function, otherwise use a procedure
6
Why are they needed? 1) Simplify big projects: 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) Eases group work: 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!!!!!!!!! (See step 3)
7
Where are they located? Procedures and functions go in between the VAR declarations and the BEGIN of the main body. Or.. Between the TYPE and the VAR section of the main body.
8
How does it work? It takes three parts to make a subroutine
The Call Statement The Header The Code
9
The Call Statement This starts the procedure.
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.
10
Calling Procedures ProcedureName(ArgumentList);
Argument List: Info for the procedure How much money to add to an account How many times to repeat something in the procedure. 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)
11
Sample Procedure call statements
Procedure name No arguments in this example. Begin FirstVerse; Withdraw(Money); Show( Date, Account, Action, Amount,Newbal); Chorus; FieldGame(bet, balance); Play(song, 5); DoubleEm(total, 2*8); End. Argument List
12
The Header This defines the name of the subroutine and the name and types of the variables sent to the subroutine (parameter list).
13
Procedure 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. Start with a letter Then numbers and characters No reserved words No spaces and punctuation It should describe what the procedure does.
14
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. We will look more into parameters next class.
15
Parameter list. More on this later
Sample Headers Procedure Chorus; Procedure FirstVerse; Procedure Deposit(Account:string;var SavingsBalance:real;amount:real); No parameters Parameter list. More on this later
16
Quick Review Procedures are like mini programs
Procedures have three parts Call statement Name(Arguments) Header Procedure Name(Parameters:types); Code
17
The Code Locally declared variables
Can include const, type, and var declarations (Just like a program, but can’t have uses.) The variables only exist while the procedure is running Can use all the constructs: while loops, if, repeat,… Can call other procedures/functions that are declared above the subroutine
18
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
19
Summary Take a look over your notes and write a brief summary about today’s material. Vocab Ideas Questions
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.