
Functions with parameters (programming)

Upgrade for more content
True or false? A value sent to the function is called an argument.
Michael has a summer job. He gets seventy-two binogis an hour in fixed pay, and one point seven binogis as a bonus for each ice cream he sells. He wants to calculate how much he earns after every week he works. Let’s help Michael by writing a program. So that he can just enter the number of hours he worked, and how many ice creams he sold, and the program will calculate the weekly pay.
One way to write this program is to use a function. Let’s give this function the name "weekspay". The function should use two values: The number of hours Michael worked and the number of ice creams he sold. In the main program, we save the number of hours worked in the variable "hours", and the number of ice creams sold in the variable "icecreams". We want the main program to call the function..
Then the function uses the variables "hours" and "icecreams" to calculate the weekly pay. It is not the actual variables that are sent to the function, but the values that the variables have, each week. These values are input by typing the names of the variables in the call.. ...separated by a comma, like this. A value sent from the main program to the function is called an argument. The arguments are sent to the function “weekspay”.
Each argument that a function receives needs to be saved, so it can be used. So we create - declare - two variables, "a" and "b", inside the parentheses of the function. Now the function can receive and save the two numbers. A variable written inside a function’s parentheses is called a parameter. The function "weekspay" for instance has two parameters: "a" and "b".
Now we will calculate the pay using the two parameters. Let’s declare the variable "pay".. and give it.. the value in variable "a" times.. the hourly pay, which is seventy two. ..plus the value in variable "b" times..
one point seven per ice cream. Finally, the function outputs the weekly pay. Let’s test the code with some easy values, to see if it calculates correctly. The program asks for the number of hours worked. Let’s say it was ten hours.
Ten is saved in the variable "hours". Then the program asks for the number of ice creams. Let’s input ten here as well. Ten is saved in the variable "icecreams". When the function is called, the values saved in "hours" and "icecreams" are sent to it as arguments.
In the function, there are two parameters "a" and "b". The parameters receive the arguments. "a" is assigned the value ten and "b" the value ten. The function calculates "a" times seventy two plus.. "b" times one point seven.. and saves it in the variable “pay”. Seven hundred and twenty plus seventeen equals..
seven hundred and thirty-seven. Finally, the function says "you get seven hundred and thirty-seven binogis". And that is correct. So the main program makes a call to the function. In the call, the main program sends the values - the arguments - to the function.
The function receives the various arguments. And saves them in their parameters. The function then uses the parameters... .. to calculate Michael's weekly pay. Now Michael can calculate his weekly pay really quickly!
The time he saves he can spend… eating ice cream!