When returning a variable from a function, the data type of the variable that is being returned must match the function type. If the function type is a void , then no variable can be returned.
This can be a very basic mistake to make, but it is very important to check this before deploying your code. A lot of times, people make this mistake when using other languages, so it is important to know how to check for this error in Java.
When executing a method in Java, if no value is returned, then nothing happens and the program ends. This makes checking for errors difficult unless you use exceptions. Exceptions are covered in more detail later on in this article!
There are three basic data types in Java: primitive types, objects, and exceptions. This article focuses solely on primitives and objects, how they relate to returning values from functions, and how to correctly check for these errors.
The data type of a variable must match the function type

Variables can be declared in two ways: with a variable declaration or with an assignment.
A variable declaration occurs when you define the name and type of the variable, but do not give it a value. For example, the following code declares two variables: one of type integer and one of type string.
Integer intVariable = 5; String stringVariable = “hello world”;
In both cases, the variables are declared but not initialized. When this happens, Swift creates an empty value of that type. The integer intVariable is set to zero, and stringVariable is empty.
Assigning a value to a variable can happen in two ways: by assigning it a value directly or by using a function that returns a value. Let’s look at each of these cases separately.
Return statements and types

A return statement is used to exit a function before it runs its full course. A return statement takes the form of the word “return” followed by a value.
For example, if a function calculated the total cost of an order based on the order amount, the function would need to return the total cost as well as notify the code that sent it that amount that it completed its task.
The code that sent it, in this case, would be something like a cart application. The cart would ask for the total cost of the items and then send it to a processing service which would then calculate costs.
A return type defines what kind of data is returned by a function. A return type can be any data type except void. A void type indicates that no value is returned which is not allowed in functions.
To understand this more, let’s look at some examples.
Example of a return statement and type

A return statement ends execution of a function before normal completion. A return statement takes a single expression as a parameter, which is evaluated and returned to the location that called the function.
A return statement can be used in two situations: when the function completes its task or when an external condition is met. When the function completes its task, then the return value must match the type of the function.
For example, if a user enters their username and password into a login function, then the only valid return value is a boolean — true if they authenticated successfully, false if not.
Understanding return statements and types

When a function executes a return statement, it is returning either a value or no value. If the function completes its task without any issues, it returns a value.
If the function encounters an error such as being given invalid input, it returns no value. If the function executes all of its code and decides that there is no valid output, it also returns no value.
The type of return must match the type of the returned value. A return statement with no value will throw an error if the type is not compatible with a void type.
For example, if you have a function that returns an integer and you try to assign that integer to a variable of type string, then JavaScript will throw an error. Integer values cannot be assigned to strings correctly because they are not the same type.
This can be confusing when trying to debug code due to unexpected errors.
What are the implications?

This may seem like a small detail to focus on, but it can have significant consequences.
If you try to return a value of one data type through a function that returns a different data type, your code may not work as intended.
For example, if you have a function that returns an integer and you try to return a string from that function, PHP will not complain, but it will instead treat the returned value as an integer.
Furthermore, if you try to assign a string value to a variable of type integer, again PHP will not complain, but your program may produce unexpected results. For instance, if you attempt to assign the result of the above mentioned function call to an integer variable, its value will be zero because the string was converted to zero before being assigned to the variable.
What are the alternatives?

There are a few ways you can deal with this situation. You can use what is called a null object, null reference, or null value.
A null object means the code runs but there is no object to work with. For example, if you try to call a function that returns an object and you assign that return value to an object variable then there is no object stored in that variable. The code will run but it will have no effect.
A null reference means the code does not run because there is no reference to anything. In cases where you have a return statement that returns nothing then the code runs but nothing happens because there is nothing to reference.
A null value means the code sometimes runs but has no effect because it is set to zero or false. This can sometimes cause issues later on when the code tries to use the variable but it is set to nothing.
Why is this important?

Because JavaScript is a language that executes in runtime, it requires mechanisms to know what kind of data a variable contains and how to use that information.
If a return statement contains a variable of any data type other than undefined or null , then the returned value is assumed to be a string. If the returned value is not a string, then an error is thrown.
or , then the returned value is assumed to be a string. If the returned value is not a string, then an error is thrown function myFunction() { return 10; } myFunction() === 10; // returns true However, if the returned value is undefined or null , then the returned value is assumed to be false . If the function returns any other type of boolean, then an error is thrown. function myFunction() { return true; } myFunction() === true; // returns false function myFunction2() { return false; } myFunction2() === false; // returns false These errors can be hard to catch because they are not necessarily obvious. It’s important to know these quirks so that you do not spend too much time debugging your code!
How Can I Avoid This Issue?
Make sure that the data type of your return statement matches the function type! When your code catches all possible errors and resouses from all possible errors, you can be sure that your code will run smoothly.
The best way to do this is by using if…else statements with strict checks on all possible types of errors. For example, you can check whether or not a user has logged in by using an if statement with two paths: logged in or not logged in. Then, you can use an else statement with another if statement where you check for all other possibilities (such as failure to log in due to incorrect credentials).
Summary

When returning a variable from a function, the data type of the variable in the returned statement must match the function type. If the function returns a value of type A, then the returned variable must be A.
Variables can be bound or unbound when being returned. Unbound variables are declared with no assignment. Bound variables are declared and assigned a value.
When a bound variable is being returned, its assigned value is copied into the returning variable. Unbound returning variables are set to null or undefined depending on what kind of variable it is.
It is important to know that this rule applies to all JavaScript types: Boolean, Number, and String! All of these require their own special attention when being returned from a function.


