AP CSP Topic 3.13 Coding Practice - Developing Procedures
Big Idea 3: Algorithms and Programming · Topic 3.13 · Coding Practice
Developing Procedures — Code It Yourself
Write real code and check it against the expected output. Pick Python or JavaScript; the AP pseudocode reference is there when you need it.
You warmed these up on paper in the Trace the Procedures and Design the Procedure exercises — the school store's labelTotal and charge. Now build and run them for real, hardest last. The first problem is the procedure you just traced; the last one you write from scratch. Predict each output first, then check yourself.
Problem 1 of 4
Write a procedure labelTotal(unitPrice, quantity) that RETURNS unitPrice times quantity. Then print the total for a product priced at 3 with quantity 12, which should be exactly: 36
The procedure should RETURN the product, not print it — the caller does the printing. RETURN hands the value back so print(labelTotal(3, 12)) can display it.
Problem 2 of 4
Reuse ONE procedure for many products. Using labelTotal(unitPrice, quantity) from before, print the combined total of two products: pencils at price 3 quantity 12, and erasers at price 2 quantity 30. The combined total should be exactly: 96
Define the procedure only once, then call it twice with different arguments: labelTotal(3, 12) gives 36 and labelTotal(2, 30) gives 60. The parameters let one definition serve both products; add the two returned values.
Problem 3 of 4
Add an early RETURN guard. Write charge(price, quantity) that RETURNS 0 immediately when quantity is 0 (a free sample), and otherwise RETURNS price times quantity. Print charge(5, 0) and charge(5, 3) on one line, separated by a space, so it reads exactly: 0 15
The guard RETURN runs first: when quantity is 0, return 0 and the procedure stops — the multiply line never runs. Otherwise the second RETURN gives price * quantity, so charge(5, 3) is 15.
Problem 4 of 4
Write from scratch: discountedPrice(price, discount) returns price minus discount, but a discount can never push the price below 0. If discount is greater than or equal to price, RETURN 0 immediately; otherwise RETURN price - discount. Print discountedPrice(20, 5) and discountedPrice(20, 25) on one line so it reads exactly: 15 0
Use an early RETURN for the special case: if discount >= price, return 0 and stop. Otherwise return price - discount. discountedPrice(20, 5) is 15; discountedPrice(20, 25) hits the guard and is 0.
Your code runs on a secure external service. Answers are checked automatically — nothing is stored.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed.
Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Typically responds within 24 hours
✓
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.