Return statement facts for kids
A return statement is like a message or a result that one part of a computer program sends back to another part. Imagine you ask a friend a question. Their answer is like the "return statement." It's the information they give back to you.
In computer programming, especially in a style called object-oriented programming, when a part of the code (called a method) finishes its job, it often returns something. Think of a vending machine: when you press a button for a drink, the machine returns your drink. In the same way, when a computer program uses a method, something is often returned. What exactly is returned depends on something called a return type.
What is a Return Type?
A return type is a very important part of any method in programming. It tells the computer exactly what kind of information the method will give back. It's like telling your friend, "I need a number as an answer," or "I need a word as an answer."
For example, if a method is supposed to give back a whole number (like 5, 10, or 100), its return type would usually be `int`. This is a common short name for "integer" in computer science. There are many different return types available in most programming languages, each for a different kind of data.
Integer Return Type
When a method has an integer return type, it means that the method will give back a whole number. This number won't have any decimal points. For example, if a method calculates how many apples are left, it would return an integer like `3` or `0`.
Double Return Type
A double return type means the method will give back a number that can have decimal points. This is useful for calculations where you might need more precise numbers, like measurements or money. For instance, if a method calculates the average temperature, it might return a double like `25.5` degrees.