
Functions with return values (programming)

Upgrade for more content
True or false? We give the function a name so we can call it in the program.
Lina has merged together two small robot programs, into one bigger one... ... by using functions. She converted two sections of her code into two functions ... ... and the part of the program that uses the functions, her main program, looks like this: The first instruction is a call to the Start function. Then two different people will input their name and their age.
The program calculates the age difference, and the robot tells the result. Lastly in the main program, the "End" function is called. Lina tests the program. The robot performs the instructions in "Start": Open eyes, Spin the antenna and turn on the screen. Then the program returns from the Start function to the main program.
It reads the line that comes immediately after the call to the Start function. The robot asks Lina to input her name ... ...and age. Then comes the instruction "Ask the user to enter their name" But we already had that instruction! These five rows are almost the same as.. ..these five rows, ..except that first, the variables "name-one" and "age-one" are used, and then the variables "name-two" and "age-two". When code is repeated, Lina can use a function; she already learned that!
But the code is not exactly the same in both places. The names of the variables are different. Will it still work to use a function? Yes, it will work, actually. Check this out.
The instructions for the function already exist: here! Lina copies the rows and places them above the main program. What she needs is for this function to work for all names and ages, not just name-one and age-one, or name-two and age-two. How does she do that? She changes the variables to just ‘name’ and ‘age’ - she takes out the numbers.
The numbers will be added when the function responds to the program's call. And lastly in the function, Lina puts the instruction "Return name and age". The function then sends back, returns, the value contained in the "name" and "age" variables to the main program. The new function gets the name "Hello" with two parentheses. Lina looks in the main program.
The program should compare the ages of two people. So she calls the Hello function two times. Like this. And also here. The first time the function is called, "name" and "age" are to be saved in the variables "name-one" and "age-one".
Therefore she writes this. “Name-one and age-one call hello” She assigns the values to the variables with an equals sign. And the other person's name and age are saved in "name-two" and "age-two". Did you see what she did? One single function was used to save values in several different variables. Lina tests the code again.
The program starts by calling the "Start" function. The robot wakes up. The program returns to the row after the call. Now the "Hello" function is called. And the robot asks for Lina's name. "Lina" is saved in the variable "name". - Hello Lina The robot asks for Lina's age and she answers 16. "16" is saved in the variable "age".
The values noted in the variables "name" and "age" are returned to the row that called the function. "Lina" is saved in "name-one" and "16" in "age-one". The next row in the main program calls "Hello" again. The robot asks for Maria's name. "Maria" is saved in the variable "name". - Hello Maria The robot asks for Maria's age and she answers 15. "15" is saved in the variable "age". The values noted in the variables "name" and "age" are returned to the main program. And NOW, "Maria" is saved in the variable "name-two" and "15" in "age-two".
The next row in the main program saves the result of "age-one" minus "age-two". And then the robot says - Lina is one years older than Maria. The program works! The "Hello"-function can return different values that are stored in different variables using return values.