Jump to content
Sign in to follow this  
nindall

Trouble with function

Recommended Posts

As a part of some scripting involving trigonometry I wanted to write what I thought would a simple function to convert a negative number into a positive or vice-versa.

PosNeg.sqf

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_n"];

_n = _this select 0;

_n = (_n - (_n * 2))

From a dialog text-box (If that makes any difference) I preprocessed and then tried to call it using -

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

hint format ["%1",[37] Call PosNeg]

It dosn't return any errors but prints '<null>'.

I also tried putting brackets around the '[37] Call PosNeg'.

Thanks in advance.

Share this post


Link to post
Share on other sites

Solved! By adding another line containing the result -

PosNeg.sqf

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

private ["_n"];

_n = _this select 0;

_n = (_n - (_n * 2));

_n

Can anyone clarify why this is necessary? I thought functions could end with an assignment?

Share this post


Link to post
Share on other sites

I guess you have to return just the var. All operations, including the last assignment require a semi-colon.

--Ben

Share this post


Link to post
Share on other sites
Quote[/b] ]I thought functions could end with an assignment?

They can, but it would have to be written like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private ["_n"];

_n = _this select 0;

(_n - (_n * 2))

Share this post


Link to post
Share on other sites

Let's shorten it a bit:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_this - (_this * 2)

Then call it without the brackets around the number:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">hint format ["%1",37 Call PosNeg]

Explanation: you already have _this variable holding the number, no need for another variable _n.

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
Sign in to follow this  

×