- Home /
Why pass parameters into a function?
Hi, all! I know I just posted, and I don't mean to be a pest, but I have another totally separate question.
Why would I pass parameters into a function? This may sound silly, but wouldn't:
var start = 0.1;
function MakeStart(){
Debug.Log(start);
}
function Update(){
MakeStart();
}
Be exactly the same as:
var start = 0.1;
function MakeStart(number : float){
Debug.Log(number);
}
function Update(){
MakeStart(start);
}
? It seems to me that they are identical. So what is the advantage of passing parameters into a function?
Answer by Dragonlance · Aug 06, 2012 at 08:28 PM
Hi!
You use functions with parameters i.e.
a) If you want to call a public the function from another class.
b) If you want to reuse a function and make the rest of the code more readable.
c) If you do not want to allocate the memory for the parameter all the time.
d) Some other cases
Example for a)
You hava a rocket that hits a collider with a Script called Enemy. The enemy has an applyDmg function that you call from the other script:
public function applyDmg(value : float) {
health = health - value;
}
Example for b) is taken from the MouseOrbit Script
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
{
distance -= Time.deltaTime *10.0f;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
{
distance += Time.deltaTime * 10.0f;
}
var position = rotation * Vector3(0.0, 0.0, -distance) + target.collider.bounds.center;
transform.rotation = rotation;
transform.position = position;
}
}
function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Example for c) is only called once the lifetime of the object, so why waste memory and keep the variable as class member. On the other hand if it is called in the update Function, I would do it like your first piece of code.
var start = 0.1;
function MakeStart(){
Debug.Log(start);
}
function Update(){
MakeStart();
}
There are other cases. But you are right, often it is wise to have a member variable in the object because if you would call your MakeStart every Update(), there will be a lot of overhead creating a new variable and passing it to the function.
So I would say the conclusion is: Sometimes you really need parameters in functions because you can not send your data otherwise. Sometimes it is wiser to use parameters, because your code will be better to read and cleaner. Sometimes it is a trade between speed and amount of memory uses - so you have to decide what will be better performing.
Hope that helps =)
I should point out that passing a variable in a parameter doesn't allocate heap space (just stack space) which helps from a garbage collection point of view. But otherwise you are right, no point copying variables around it all takes time.
I think if the variable is not global then you would have to pass it since it does not exist at compilation.
Ex:
if(smg){
var aVariable:float = 22;
Fct(aVariable);
}
function Fct(v:float){
print(v);
}
Thank you for all of the lovely answers! I understand the use of it now. Just bare with me here: If I want to have an Object Lerp one thing or another I usually end up making and re-making variables used in other scripts. Are you saying that ins$$anonymous$$d of completely re-writing it, I can create a public function in a script, attach it to an empty that I just dont destroy on load, and I will be able to access that function and utilize it as long as parameters are fed to it?
Yes you could do something like a Utility Library. You will even not have to take care about "destroy on load" - This is only usefull if you want to store data of some type and use it from one scene to the other.
I am not completely sure about utility classes in javascript, because I use C# mostly. $$anonymous$$aybe in js you will be forced to attatch it to an object, because all scripts are a inherit from $$anonymous$$onoBehaviour in unitys javascript (but i am not 100% sure about this)
You can call functions like this from everywhere:
static function $$anonymous$$ultiply(float x, float y) : float {
return x*y;
}
Say this is in a File called $$anonymous$$yFunctions.js
Then you can just call
var result = $$anonymous$$yFunctions.$$anonymous$$ultiply(10.0, 15.0);
Wow you just made my life. I'm sorry this is the very last question. Your examples are great, but I feel like your 'a' example is worded strangely. When you say 'the other script' are you talking about a script called 'Enemy', or are you saying that 'Enemy' contains the function and that the script attached to the rocket calls it and the rocket is ultimately affected by this?
Answer by whydoidoit · Aug 06, 2012 at 07:58 PM
Parameters enable a function to operate in many different ways or on different values without cluttering your code up with endlessly more variables that are used for just one purpose which can get confusing very fast. For instance it wouldn't be easy to call the same function twice as part of a calculation if they didn't have parameters.
var numberOne : float;
var numberTwo : float;
function AddTwoNumbers()
{
return numberOne + numberTwo;
}
function Update()
{
//Works but verbose and messy
numberOne = 10;
numberTwo = 20;
Debug.Log(AddTwoNumbers());
//Lets say we want to call the function on the result
//to double it - this doesn't work
numberOne = AddTwoNumbers();
numberTwo = AddTwoNumbers();
Debug.Log(AddTwoNumbers());
//So it would have to be this
var result = AddTwoNumbers();
numberOne = result;
numberTwo = result;
Debug.Log(AddTwoNumbers());
}
Perhaps this is easier:
function AddTwoNumbers(numberOne : float, numberTwo : float)
{
return numberOne + numberTwo;
}
function Update()
{
Debug.Log(AddTwoNumbers(10,20));
Debug.Log(AddTwoNumbers(10,20), AddTwoNumbers(10,20));
//Or
var result = AddTwoNumbers(10,20);
Debug.Log(AddTwoNumbers(result, result));
}
You example shows a case where you could do either - presu$$anonymous$$g $$anonymous$$akeStart is very specific there is not need to give it parameters.
Your answer
Follow this Question
Related Questions
Source code for library function "RecalculateNormals()"? 4 Answers
Array Problem - Error Code BCE0022 1 Answer
Function as parameter? 1 Answer
How to pass Predicate to a Coroutine? 1 Answer
Defining Parameters in a Function 0 Answers