saving . . . saved scilab code question has been deleted. scilab code question has been hidden .
scilab code question
Title
Question

function foo(a)
         a=a+1
         b=return(a)
         c=52
endfunction
foo(42);

assert_checkequal(43,b)

the above code is right out of the help file for return: so foo is returning the value of a as b. And the above does work.  However, when I modify the next to last line as follows:

x=foo(42), I'm assigning b into x, at least that's what I thought. Instead I get

Wrong number of output arguments. as an error.  This would have worked in C or C++, so what am I missing here?  Also in the help file there is a mention to the use of the following syntax:

\r\n

\r\n \r\n \r\n \r\n \r\n <table><tbody><tr><td valign="top">

the syntax <code class="scilabcode"><font face="Courier New"><span class="scilabfkeyword">function</span> \r\n <span class="scilabinputoutputargs">b</span><span class="scilaboperator">=</span><span class="scilabfunctionid">foo</span><span class="scilabopenclose">()</span></font></code> \r\n is recommended.\r\n Supposedly to eliminate the return.   

So suppose I modified the previous code to:


        function x=foo(a)
         a=a+1
         //b=return(a)
         c=52
       endfunction\r\n                         to get rid of the return 

</td></tr></tbody></table>\r\n

\r\n \r\n\r\nHow do I know what gets assigned to x unless there is a return(a). After all there is an a and a c in the function.  And the code doesn't work anyway.  The mention of this form of function declaration is minimal, to say the least, and to further exacerbate the matter the help utility does not show anything when I search for function. I did accidentally find some information on debugging, which saved me from making a post about where the debugger is.


Scilab Getting-Started 16-17 min 50-60 sec 16-06-18, 3:03 a.m. maxcy

Answers:

how to solve it 

function foo(a)
a=a+1
b=return(a)
c=52
endfunction
foo(42);

assert_checkequal(43,b)

kindly reply <a href="http://www.sscguide.in/jht-result/" target="_blank" title="">here</a>

16-06-18, 11:24 p.m. nehajain91
16-06-18, 11:25 p.m. nehajain91

Login to add comment


<a href="http://www.sscguide.in/jht-result/" target="" title="hello">hello</a>
16-06-18, 11:25 p.m. nehajain91


I do not know for what specific reason you would want to use functions in that manner but read the "note" in the end of the help for "return". The note reads "Note: the usage of this feature can complexify the code. Instead, the syntax function b=foo() is recommended." In other words the return() in scilab's function is not the same as the "return" in C/C++. So do not compare.

The best way to return a variable in scilab is to define it in the function definition and then call the function along with the output variable. The number of output variables called must be equal to the number of output variables defined.
18-06-18, 2:59 p.m. rupakrokade


Log-in to answer to this question.