Jump to content
BlacKnightBK

Can a function be overloaded

Recommended Posts

Hello guys,

 

So I was wondering if overloading a function is possible since I can do that in java and c++ I was wondering if it was possible to do that here as well. 

 

If yes, please tell me how.

 

Cheers

Share this post


Link to post
Share on other sites

Uh maybe I misunderstood, but overloading is being referred to executing a function with different functionality.

 

Similar to this:

TAG_fnc_myFunction = {

params [["_attribute",""]];

if (_attribute isEqualTo "HINT") exitWith {hint "Cheers!"};
if (_attribute isEqualTo "FPS") exitWith {hint format ["%1fps",diag_FPS]};
if (_attribute isEqualTo "CODRULEZ") exitWith {player setdamage 1};
if (typeName _attribute isEqualTo "OBJECT") exitWith {player setpos getpos _attribute};
hint "Funny!";

};

_overload = ["FPS"] call TAG_fnc_myFunction;

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

Yip, that is not what I meant. What I mean by overloading is creating two different functions which take different parameters but have the same name

Share this post


Link to post
Share on other sites
10 minutes ago, BlacKnightBK said:

Yip, that is not what I meant. What I mean by overloading is creating two different functions which take different parameters but have the same name

My function already does what you want, unless I misunderstood again.

When defining a function you'd overwrite any function of the same name that has been defined before.

Also what MrCopyright said.

 

Cheers

Share this post


Link to post
Share on other sites
24 minutes ago, MrCopyright said:

Why on Earth would you not just create 2 different functions? lol

I think it would be cleaner to overload a function sometimes.

19 minutes ago, Grumpy Old Man said:

My function already does what you want, unless I misunderstood again.

When defining a function you'd overwrite any function of the same name that has been defined before.

Also what MrCopyright said.

 

Cheers

 

Wouldn't that delete the first function?? Or will both of them exist and the compiler know when it should use the first and when it should use the second??

Share this post


Link to post
Share on other sites

What he means with overloading is to be able to define one function more than one time with different parameter types for each definition.

Then you can have a function which e.g. deletes the object if only an object is given, deletes a group if a group is passed or deletes all object in an array.

[soldier] call TAG_fnc_myFunction;  // deletes the soldier
[group soldier] call TAG_fnc_myFunction;  // deletes all members of soldiers group
[array_of_objects] call TAG_fnc_myFunction; // deletes all objects in the array

 

 

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, sarogahtyp said:

What he means with overloading is to be able to define one function more than one time with different parameter types for each definition.

Then you can have a function which e.g. deletes the object if only an object is given, deletes a group if a group is passed or deletes all object in an array.


[soldier] call TAG_fnc_myFunction;  // deletes the soldier
[group soldier] call TAG_fnc_myFunction;  // deletes all members of soldiers group
[array_of_objects] call TAG_fnc_myFunction; // deletes all objects in the array

 

 

 

Exactly. So is it possible?

 

Sorry, for some reason lately I find it really difficult to express myself and forgetting simple words.

Share this post


Link to post
Share on other sites
3 minutes ago, sarogahtyp said:

What he means with overloading is to be able to define one function more than one time with different parameter types for each definition.

Then you can have a function which e.g. deletes the object if only an object is given, deletes a group if a group is passed or deletes all object in an array.


[soldier] call TAG_fnc_myFunction;  // deletes the soldier
[group soldier] call TAG_fnc_myFunction;  // deletes all members of soldiers group
[array_of_objects] call TAG_fnc_myFunction; // deletes all objects in the array

 

 

That is exactly the type of function I just posted, just with different if then statements inside, depending on the input parameters.

I don't get it.

 

Cheers

  • Like 1

Share this post


Link to post
Share on other sites
2 minutes ago, Grumpy Old Man said:

That is exactly the type of function I just posted, just with different if then statements inside, depending on the input parameters.

I don't get it.

 

Cheers

 

Oh, I am so sorry. i though those if statements is your function body. I am such an idiot (sometimes).

Share this post


Link to post
Share on other sites
4 minutes ago, Grumpy Old Man said:

That is exactly the type of function I just posted, just with different if then statements inside, depending on the input parameters.

I don't get it.

 

Cheers

 

Yes it is what you posted but it is not as clean as in a language like C++.

There you can do 3 single different definitions of those function with the same name.

What you did is one definition which handles different parameter types.

 

But it should do the job for his purpose :-)

Share this post


Link to post
Share on other sites
4 minutes ago, sarogahtyp said:

What you did is one function which handles different parameter types.

Which is the only way to do what BlacKnightBK wanted considering arma and sqf, as far as I know.

The parameter handling inside the function is up to the OP how he wants the function to handle different input parameters.

 

Cheers

Share this post


Link to post
Share on other sites
2 minutes ago, sarogahtyp said:

 

Yes it is what you posted but it is not as clean as in a language like C++.

There you can do 3 single different definitions of those function with the same name.

What you did is one function which handles different parameter types.


The function in Arma is a variable containing code. So you are asking can the same variable have different value at the same time. I think the answer is obvious IT CANNOT

Share this post


Link to post
Share on other sites
1 minute ago, Grumpy Old Man said:

Which is the only way to do what BlacKnightBK wanted considering arma and sqf, as far as I know.

 

Cheers

 

Sweet, good to know.

 

I have a question though regarding this part

params [["_attribute",""]];

Is it meant to be like that or is it supposed to be like: 

params ["_attribute"];

 

Share this post


Link to post
Share on other sites
1 minute ago, killzone_kid said:


The function in Arma is a variable containing code. So you are asking can the same variable have different value at the same time. I think the answer is obvious IT CANNOT

Thank you for pointing this out. But I was not asking, just clarifying what the question was.

Share this post


Link to post
Share on other sites
3 minutes ago, BlacKnightBK said:

Sweet, good to know.

 

I have a question though regarding this part


params [["_attribute",""]];

Is it meant to be like that or is it supposed to be like: 


params ["_attribute"];

 

 

The second argument inside the bracket merely declares a default value if no input is given:

TAG_fnc_MyFunction = {

params [["_test","Hello!"]];
hint str _test;

};

_test = ["Whassup!"] call TAG_fnc_MyFunction; //displays "Whassup!" as hint
_test = [] call TAG_fnc_MyFunction; //displays "Hello!" as hint

Check out params.

 

Cheers

Share this post


Link to post
Share on other sites
Just now, Grumpy Old Man said:

 

The second argument inside the bracket merely declares a default value if no input is given:


TAG_fnc_MyFunction = {

params [["_test","Hello!"]];
hint str _test;

};

_test = ["Whassup!"] call TAG_fnc_MyFunction; //displays "Whassup!" as hint
_test = [] call TAG_fnc_MyFunction; //displays "Hello!" as hint

 

Cheers

 

THANK YOU. I NEEDED THAT.

  • Like 1

Share this post


Link to post
Share on other sites

OK i understood everything but lets say i want to have 2 ways i can put parameters:

[codX,codY,codZ,angle]
and :

[cords,angle]
with signle name of function.
is this posible ?
or again i will need set:
[_firstPara,_secondPara,_thirdPara,_fourthPara]
if(typeName _firstPara == "ARRAY") then {...

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×