How does the return of a method work?

I’m finding myself having to use the same bit of code that I completely stole from unity documentation that finds the closest object with a tag/name, then I usually find the distance between the current object and said closest object, then run blah blah blah if its within a range. I recently watched a video where someone used a method and just put return at the end of it, so now I’m curious as to how it works and if it would be beneficial in my situation.

GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Enemy");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }

I adjusted this to fit my needs for one situation, but I keep needing to copy and paste it. Could I put it in a function like:

GameObject FindNearest(GameObject current, GameObject[] lookFor){
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject object in lookFor)
        {
            Vector3 diff = object.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = object;
                distance = curDistance;
            }
        }
        return closest;
    }

The problem is I don’t know how to utilize the returned value. Is it something like

object = FindNearest(current, lookFor)

Or is it

Vector3.Distance(FindNearest(current, lookFor), current)

I’ve looked through quite a few videos but all of them are things like returns in if statements, so I couldn’t find one that matched my case. If possible, try to explain it like I just picked up a computer since I’m still very, very new and don’t know a lot of things that it might have to do with.

Either way. Both of those code samples work just fine. FindNearest returns a GameObject, so you can use FindNearest(current, lookFor) anywhere that a GameObject would normally go.

As @kdgalla said, both examples would work fine.

You can think of the return value of a method like the method itself having this type. Ie, assining it to a variable is fine, passing it as a parameter to a function is fine, or something like FindNearest(current, lookFor).position is fine as well.

When you define a return type for a method, this method has to return a value of such type in all of its possible execution paths. So say for example you had a method like so:

public int SomeMethod(Gameobject obj){

  if(something){
   // Part A
  } else {
    // Part B
  }
  // Part C
}

Assuming Part C is empty, both Part A and Part B need to contain a return statement, since otherwise there would be a possible way to execute the method which would not return a value of the return type. If nowever you return a value in Part C, then this would be executed either way and thus neither Part A or Part B have to return a value - but one or both of them could return a value.
When the first return is executed, the method finishes. So say, for example, our method is supposed to check an object for a specific feature, as our “something” condition. So if “something” is true we may already be able to return some value related to the object we are checking, otherwise we would process it in some way in Part B and then either return it there or in Part C.

In methods without a return type, ie void, you can use the return keyword to skip the rest of the method.
For example:

public void HeavyWorkload(bool skippable){

  if(skippable){
    return;
  }

  // Execute heavy workload
}
2 Likes

The only moment when you need to worry about the difference between the two is when you need to get the value at another moment, for various reasons (slow process that only need to happen once and not in update, the value you need is different at different times, and so on). In which case you can store the return value output by your method to use it later.
In any case, storing the return value in a variable or not is entirely up to you. You can also make your method “void” and modify a property of your class:

public class SomeClass
{
string someString;

private void SomeMethod()
   {
      someString = someValue;
   }
}

To have basically the same effect of using a value modified by a method. Be careful though that this usage requires you to use the method to update the value if you need an updated value.

The usage here is really specific. If you need to use any sort of jump statement, return; might not always be what you want. To know more, I strongly suggest to read about the different kinds and the effects of jump statements, like here.