Unit 3 Sections 12-13 Hacks
Notes 3.12 Notes What is a procedure and a parameter? A procedure is a named group of programming instructions that may have parameters and return values.
Procedures can be reffered to as method or function depending on the programing language.
A procedrure call interrupts an execution of statements and makes the program execute the statements in the procedure. Parameters are input values of a procedure. Arguements specifcy the values of the parameters when a procedure is called.
If you have a set of statements and inside there is a procedure then when it reaches that procedure it will excute that procedure and then go back to the rest of the statments.
If you see a return statement you automatically end the procedure even if there is more statements
Procedures are that a catch-all term for codes used to identify what was done to or given to a patient If you have a set of statements in a procedure the statement will go thought the top of the procedure to the bottom in order.
3.13 Notes Procedure: is a named group of programming instructions that serves a purpose Parameter: are input values of a procedur There are two types of procedures, one that returns a value or some type of data and on that just executes a block of statements Modularity: the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program Creating a Procedure When you are picking a name for the procedure it must be descriptive See if you need any parameters for this procedure (what data do you need to accomplish my goal? What king of information am I going to need?) 2 of the parameters in the example below could be the quizGrade and currentPoints (need this data) One common type of abstraction is procedrual abstraction, which provides a name for a process and allows a procedure to be only knowing what it doe,not how it does it. The subdivision of a computer program into sepearate subprogrames is called modularity.
3.12 Part 1 Hacks
Problem 1: This problem involves parameters
Qais is writing code to calculate formulas from his math class. He's currently working on a procedure to calculate average speed, based on this formula:
Average speed = Total Time/Total Distance
Highlight which of these is the best procedure for calculating and displaying average speed.
PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (distance/time) }
PROCEDURE calcAvgSpeed (distance) { DISPLAY (distance/time) }
PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (time/distance) }
Problem 2: Procedures with return values James Hunter is looking through his classmate's program and sees a procedure called heightenEmotions: PROCEDURE heightenEmotions(myEmotion)
Problem 2: Procedures with return values
James Hunter is looking through his classmate's program and sees a procedure called heightenEmotions: PROCEDURE heightenEmotions(myEmotion)
{ moreEnergy ← CONCAT(myEmotion, "!!!")
moreVolume ← UPPER(moreEnergy)
RETURN moreVolume }
That procedure manipulates strings using two built-in procedures, CONCAT for concatenating two strings together, and UPPER for converting a string to uppercase.
James Hunter then sees this line of code:
heightenEmotions("im mad")
After that line of code runs, will nothing be displayed?
True
False
I think 'IM MAD!!!' will display because the upper() function converts all lowercase to uppercase and the concat() function adds on three exclamation marks. Therefore something will be displayed.
Problem 3: Procedures with return values
Bubz is writing a program to calculate the carbon footprint of his activities. The procedure calcFlightFootprint calculates the pounds of carbon dioxide produced per passenger in a flight that covers a given number of miles and seats a given number of passengers.
PROCEDURE calcFlightFootprint(numMiles, numPassengers) { CO2_PER_MILE ← 53.29
carbonPerFlight ← numMiles * CO2_PER_MILE
carbonPerPassenger ← carbonPerFlight / numPassengers
RETURN carbonPerPassenger
}
Bubz wants to use that procedure to calculate the total footprint for his two upcoming flights: LA to NY: 2,451 miles and 118 passengers NY to London: 3,442 miles and 252 passengers
Which of these code snippets successfully calculates and stores her total footprint? Highlight 2 answers.
totalFootprint ← calcFlightFootprint(2451, 118) + calcFlightFootprint(3442, 252)
totalFootprint ← calcFlightFootprint(2451, 118 + 3442, 252)
totalFootprint ← calcFlightFootprint((2451, 118) + (3442, 252))
laNyCarbon ← calcFlightFootprint(2451, 118) nyLondonCarbon ← calcFlightFootprint(3442, 252) totalFootprint ← laNyCarbon + nyLondonCarbon
3.12 Homework/Hacks pt 2
- PROCEDURE find a ()
{ c -- 9
b <-- 9 * 9
a <-- b * c
Print (a)
}
What is a?
a = 729
- cost ⟵ 173 tax - 10%
PROCEDURE applytax (cost, cpercentDiscounted) { temp <-- 100 + percentTaxed
temp <-- temp / 100
cost <-- cost x temp
Print(cost)}
What is the cost?
the cost is $190.30
Temperature - 103 Degrees PROCEDURE convet Fahrenheit (tempature)
{
Celsius <-- tempature - 32
Celsius <-- Celsius x 5/9
Print (Celsius)}
answer = 39.444
if currentrbyards(1260) > toprbyardspg(100)
replace(toprbyardspg(100), currentrbyards(1260), totalGames(12))
PROCEDURE getAplus(){
If (can_MoveForward):
Move_Forward
Else (can_MoveRight):
Rotate_Right
Move_Forward
If (can_MoveLeft);
Rotate_Left
Move_forward
}
PROCEDURE BallToGreen{
Rotate_Left
Move_Forward
Rotate_Right
Move_Forward( until count = 6)
break
Rotate_Left
Move_Forward (until count=2)
break
}