Read a short program that calculates how many minutes of study time remain. You will see where its output comes from and predict what happens when you change an input.
You can read this page without programming experience or installed tools. If you already write programs, go to Coming From Other Languages or First Program.
A program that calculates time
A program is code that a computer can run. This one subtracts the time spent studying from the time planned.
function minutesLeft(total: int, spent: int) -> int {
total - spent
}
let plan = "study plan"
let total = 45
let spent = 20
print("{plan}: {minutesLeft(total, spent)} minutes left")The program displays this line when it runs.
study plan: 25 minutes leftIt gets 25 by subtracting 20 from 45. The English text says that 25 minutes remain in the study plan.
Find values and names
45, 20, and "study plan" are values, the data the program uses. Numbers can take part in calculations. Text goes inside quotation marks.
A name lets you use a value later. let total = 45 gives 45 the name total. The next line gives 20 the name spent. The name plan holds the text "study plan".
The last line uses print to display text. Inside the quotation marks, {plan} inserts the value named plan. The other pair of braces inserts the result of the calculation.
Follow the function
A function groups code that you can call by name. The first three lines define minutesLeft. When the last line calls minutesLeft(total, spent), it passes 45 and 20 into the function.
Inside the function, total - spent subtracts the second number from the first. This is the final expression in the function, so its value becomes the result. Here the result is 25. The same function can calculate a different result when you give it different inputs.
The int in the first line means an integer, a number with no fractional part. A type describes the kind of value a program accepts. total: int and spent: int require integer inputs. -> int says the function returns an integer.
This function calculates its result from the two numbers passed to it. You can follow this calculation by reading its inputs and the subtraction.
Check and run the file
A run makes the computer carry out the code. To try this example, install Topaz using First Program and save the code as what-a-program-is.tpz. Open a terminal in the folder containing that file and enter these commands in order.
topaz check what-a-program-is.tpz
topaz run what-a-program-is.tpzFor this file, check reports types-ok and resolve-ok. The run command displays study plan: 25 minutes left, as shown above.
Values passed to a function are called arguments. In minutesLeft(total, spent), total is the first argument and spent is the second.
topaz check finds type mismatches, unknown names, and missing arguments before execution. Calling minutesLeft(total) without the second argument reports an error at that call. Checking does not determine whether the program implements the logic you intended. Test the results with inputs whose answers you know.
If you try the one-argument call and see that error, put spent back as the second argument. Check the file again before running it.
Try this
Change let spent = 20 to let spent = 50, leaving the other lines unchanged. Predict the output before running the file again.
Answer
The output becomes study plan: -5 minutes left because 45 - 50 is -5. This program allows a negative result. If your study plan needs a different response when time runs out, you must write that rule in the code.
Ready to continue when
You can find the values 45 and 20, follow them into minutesLeft, and explain why it returns 25. You can also tell which command checks the file and which runs it.
Continue with First Program to create and run your own file.