I hope this could help



I was looking that fellows begin to struggle with this class so I decide to share my notes with you hoping this can contribute something in your study.

Disclaimer: I’m a C# (and Unity) dummy but this steps help me to understand (it took me a lot to understant too) how parameters and variables work (unless in this early stages).

I make this simple example to get a better outlook. (I wrote it without look at my notes after frying my brain :P)

If I’m wrong at something please don’t hesitate to tell me asap!!


This is cool. Well-done!

My education on the topic is informal, but I see nothing wrong with your notes. To add a concept about functions (it seems like you’re ready for more)… Where your function has the term ‘void’ is what is called the ‘return type’. You see that you can hand a parameter (or parameters, plural) to your function, well the function can hand a value back too. I give you the following example:

   float Speed(float distance, float time)
      if (time > 0) return distance / time;
      else return -1;

The function above takes two float values, and it returns a single float value. To avoid trying to “divide by zero” I gave it the IF check. It’s not necessary to have it return -1 when time=0, but it can be a handy way to communicate an error or irregularity… A use-case might look like:

      float someDistance = 45.0f; // shall we say, feet?
      float someTime = 37.6f; // shall we say, seconds?
      float someSpeed = Speed(someDistance, someTime); // result would be in feet per second.
      Debug.Log(someSpeed + " feet/second");

I hope that makes sense. We built a simple function to calculate speed, which is distance/time. We fed it a couple made-up values with made-up units, and it returned a simple numerical value; we know the units, because it’s based on what we fed it… in this case, feet/seconds.

So, do you want to take it to the next level? Let’s say we want a quick way to calculate speed in MPH in addition to the simplified Speed function we already have. I’m glad you asked, let’s see what that might look like:

float FeetAsMiles(float feet)
      return feet / 5280;

float SecondsAsHours(float seconds)
      return seconds / 3600;

Well, with these functions for conversions, we can actually call speed and instead of feeding it the feet units and seconds units “raw”, we can plug those units into the unit-converters, and plug those functions directly into the speed function, like this:

      float speedAsMPH = Speed(FeetAsMiles(someDistance), SecondsAsHours(someTime));
      Debug.Log(speedAsMPH + " MPH");

Fun, right ? ! ? ! ? Keep at it, you’re doing great from what I can see!

For what it’s worth, the concept can even be pushed to what is called a recursive function; one that calls itself:

   int Factorial(int n)
      if (n <= 1) return 1;
      return n * Factorial(n - 1);