Why are variables defined in argument vs function body?

I realised i do not understand why functions have variables in them vs being in the functions body. I’ll see SomeFunction(int x)
{
some code
}
is this a preference or why not declare all in the functions body?

Variables in the function declaration are for passing values through to the function. In the following example you can pass an integer through to the function and the function will be able to access it. There is also a temporary float available until the function ends.

void Foo(int x)
{
   float y;
}

Variables in the actual function are for temporary use by the function and cannot be passed through to it. In the following example you cannot pass an integer to the function, but it will have a temporary one, in addition to the temporary float, that exists until the function ends.

void Foo()
{
   int x;
   float y;
}

If you declare all variables in the function body, how do you intend on giving input to the function?

ahhh ok. So couple things.

  1. why not declare them outside the function but within the class still?
  2. some functions I have require tons of variables that need to keep updated values. do I stuff them all in the function declaration?

I usually put all variables at the beginning of my script outside the functions and then the separate functions I write within that class grab what they need. Should I be putting only the variables those functions use in the function declaration and any variable that should only exist for the functions length, within the function body?

Class/Struct level fields are for data that describes the class/object.

Method level variables are for data that pertains only to the function.

Parameters for a function are for passing in values to the function.

Example using a very basic struct of a Vector2:

Class/Struct level fields

The fields of a struct/class describe the struct/class. In this case a Vector2 contains a x and y variable. That’s what describes a Vector2! The portion of memory reserved for the object/value is there until otherwise removed. As long as you Vector2 exists, your x and y variables of it exist.

public struct Vector2
{
    public float x;
    public float y;
}

Method level variables

Method level variables are used temporarily in a method/function. They hold temporary data that may be calculated during the process of the algorithm of the method. They exist on the stack, and when the function finishes, the portion of memory used by those variables is removed from the stack.

Note how I calculate the magnitude, then use it for stuff to decide how the algorithm works out when normalizing.

public struct Vector2
{
    public float x;
    public float y;
  
    public void Normalize()
    {
      float magnitude = Mathf.Sqrt(x * x + y * y);
      if ((double) magnitude > 9.99999974737875E-06)
      {
         this.x /= magnitude;
         this.y /= magnitude;
     }
      else
      {
          this.x = 0f;
         this.y = 0f;
     }
    }
}

Parameters

Where as in this method I use the parameters to do stuff. The parameters exist on the stack just like method level variables, and will be purged from the stack as soon as the method returns. But instead of the method itself setting the values of those variables, the calling code can set the values of it to change the way the method works.

In this case, it allows us to pass in the values that modify x and y.

I can say ‘Set(1,2)’ or ‘Set(5,6)’ and get different outcomes.

public struct Vector2
{
    public float x;
    public float y;
  
    public void Set(float a, float b)
    {
        this.x = a;
        this.y = b;
    }
}

Note parameters allow you to accent the way a method works. Also, a static function doesn’t have a class state to pull variables from. Example:

public static float Dot(Vector2 a, Vector2 b)
{
    return a.x * b.x + a.y * b.y;
}

Dot relies on the parameters passed in. The return value of it is dependent on that. How else is this function supposed to get the values it’s going to calculate based off of?

3 Likes

Better performance, and declaring everything as a class variable is generally bad programming that’s more bug-prone. Keep everything as local as possible.

–Eric

3 Likes

Ok, so I’m starting to have an ah ha moment.

Th next one is realizing this is what the function chapters in textbooks are about. Those 30 pages with terms, examples, design principles; they seemed boring at first. But they’re exactly the stuff you’re wanting to know. And since someone had time to add more examples, improve them, move things around, make sure they didn’t leave out anything important … they might be better.