Return type

Functions that you create to perform a specific task may not simply carry out instructions, but instead return information. For example, if we needed to create a function that carried out arithmetic and returned it, we could write something like the following code snippet:

float currentAmount;

float DoSums(){
float amount = 5.0f + 55.8f;
return amount;
}

void Update () {
if(Input.GetButtonUp("Jump")){
currentAmount = DoSums();
}
}

Here we have a custom function called DoSums() that we have given a return type, like a data type in a variable declaration, which means it will return data of the float type. In order to get this function to return a value, we have given it this type and also used the return command, which means that when this function is used in another part of the script, its resultant value is returned, much like using a variable but with more power, as its value may change depending upon what occurs within the function.

In the example given, a sum is calculated and assigned to the currentAmount variable when the player releases the Spacebar (the default for Input "Jump"). So, in this simple example, currentAmount will be set to 60.8, as this is the returned value of this function.