Variables and Functions {Tutorial Question}
Hi Everybody!
I'm just new with Unity and I was trying lo learn something from a tutorial. This one:
https://unity3d.com/learn/tutorials/topics/scripting/variables-and-functions?playlist=17117
And here's the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
int Mario = 5;
// Use this for initialization
void Start () {
Mario = MultiplyByTwo(Mario);
Debug.Log (Mario);
}
int MultiplyByTwo (int Paolo){
int calcolo;
calcolo = Paolo * 2;
return calcolo;
}
// Update is called once per frame
void Update () {
}
}
I gave random names to variables just to see if I got the meaning. I've also understood everything in the the tutorial except for one thing: I still don't understand how in this function:
int MultiplyByTwo (int Paolo){
int calcolo;
calcolo = Paolo * 2;
return calcolo;
}
the temporary variable "Paolo" got the value of 5. I'm pretty sure it got it from this variable:
int Mario = 5;
But how?! The console at the end prints 10 as number.
Thx for your helps.
Answer by GregoryFenn · Aug 05, 2018 at 11:58 AM
You define Mario as an int, and assign it 5. You then doubled it to get a new int, 10. When you passed Mario into that method, the VALUE (not the reference name) of Mario (5) is used to assign the internal value of Paolo.
Paolo doesn’t really mean anything, it is created in the function merely to provide a save storage space that the function takes to input.
Paolo has the same status as calcolo really, as far as the function is concerned.
So what's really matter is the fact that I've assigned "int" onto $$anonymous$$ario, right? If yes, I've only have a last question: "What will happen if I will assign 2 integers? How can I decide which one the fuction will chose? For example:
int $$anonymous$$ario = 5;
int Carla = 10;
Thanks so much! Your answer was already helpfull for me :)
Ohh maybe I got the answer by myself... Do i get it from:
$$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);
right?
So i could both run:
$$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);
Carla = $$anonymous$$ultiplyByTwo(Carla);
I think you've almost got it!. In C#, and most object-orientated program$$anonymous$$g languages like Python or C++, is makes sense to write.
$$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);
or
x = x + 1;
where both x and $$anonymous$$ario are earlier (i.e. above in the code) defined as some integers, e.g. 5 and -7.
So yes, you started by setting $$anonymous$$ario to 5, and then by running the code
$$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);
you simply found a new number that is double $$anonymous$$ario (10 (=2*5)) and then immediately reassigned $$anonymous$$ario to this new number.
If you were to run that line of code again, $$anonymous$$ario would thereafter be 20.
Your answer
Follow this Question
Related Questions
Multiple variables in a function? Like function test (number : float , number1 : float) 0 Answers
Passing variable name into function from different script/animation event editor 0 Answers
How to pass a game object through to a function? 2 Answers
How to run a function without certain input variables? 0 Answers
How can you add a function to a variable. Such as string.ToUpper() ? 1 Answer