{"id":304,"date":"2014-09-21T14:19:05","date_gmt":"2014-09-21T19:19:05","guid":{"rendered":"http:\/\/www.amibroker.com\/kb\/?p=304"},"modified":"2014-12-03T07:16:39","modified_gmt":"2014-12-03T12:16:39","slug":"a-function-with-multiple-return-values","status":"publish","type":"post","link":"https:\/\/www.amibroker.com\/wordpress\/kb\/2014\/09\/21\/a-function-with-multiple-return-values\/","title":{"rendered":"A function with multiple return values"},"content":{"rendered":"
A typical AFL function returns one value. For example sin( x )<\/em> returns sine value of argument x. Sometimes however it is useful and\/or required to return more than one value from the function.<\/p> Returning multiple values is possible only via arguments passed by reference, but trouble is that in AFL all arguments are passed by value (as in C language). Passing by value means that only value<\/strong> of variable is passed, not the variable itself, so original variable is not modified as shown in the example below:<\/p> The behaviour shown above is desirable because we usually want the function to be opaque and do not interfere with what is defined outside of the function except for returning the result of the function.<\/p> But what if we actually wanted to write to variables passed as arguments? Well that is possible if we pass the names of the variables<\/strong> as arguments.<\/p> Of course we can use arguments passed by name for two-way communication – we can use them as both inputs and outputs as shown in the following example that swaps the values of arguments<\/p> The code above will produce output like this:<\/p> Before swap x = 5, y = 37 So it is clear that variables were passed to the function, swapped and returned successfully.<\/p>","protected":false},"excerpt":{"rendered":" A typical AFL function returns one value. For example sin( x ) returns sine value of argument x. Sometimes however it is useful and\/or required to return more than one value from the function.Returning multiple values is possible only via arguments passed by reference, but trouble is that in AFL all arguments are passed by […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[53],"_links":{"self":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/304"}],"collection":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":1,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":776,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/posts\/304\/revisions\/776"}],"wp:attachment":[{"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.amibroker.com\/wordpress\/kb\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<\/span>function <\/span>Dummy<\/span>( <\/span>x <\/span>)
{
<\/span>x <\/span>= <\/span>7<\/span>; <\/span>\/\/ x is treated as function-local
<\/span>}
<\/span>\/\/
<\/span>val <\/span>= <\/span>10<\/span>;
<\/span>Dummy<\/span>( <\/span>val <\/span>);
<\/span>printf<\/span>( <\/span>"%g\\\\n"<\/span>, <\/span>val <\/span>); <\/span>\/\/ will print 10 because 'val' is unaffected by function cal<\/code><\/span>\/\/ This example shows how to return multiple values
\/\/ the idea is to pass the name of the variable instead of
\/\/ value
\/\/
<\/span>function <\/span>fun_multiple_results<\/span>( <\/span>result1name<\/span>, <\/span>result2name <\/span>)
{
<\/span>VarSet<\/span>( <\/span>result1name<\/span>, <\/span>1 <\/span>); <\/span>\/\/ setting variable using passed name
<\/span>VarSet<\/span>( <\/span>result2name<\/span>, <\/span>2 <\/span>);
return;
}
<\/span>\/\/
\/\/ to get multiple values from a function
\/\/ we call the function passing NAMES of variables
\/\/
<\/span>a <\/span>= <\/span>10<\/span>;
<\/span>b <\/span>= <\/span>20<\/span>;
<\/span>printf<\/span>(<\/span>"a = %g\\\\n"<\/span>, <\/span>a <\/span>);
<\/span>printf<\/span>(<\/span>"b = %g\\\\n"<\/span>, <\/span>b <\/span>);
<\/span>\/\/
<\/span>fun_multiple_results<\/span>( <\/span>"a"<\/span>, <\/span>"b" <\/span>); <\/span>\/\/ pass the names of variables
\/\/
<\/span>printf<\/span>(<\/span>"a = %g\\\\n"<\/span>, <\/span>a <\/span>); <\/span>\/\/ see new values assigned to variables
<\/span>printf<\/span>(<\/span>"b = %g\\\\n"<\/span>, <\/span>b <\/span>)<\/code><\/span>function <\/span>Swap<\/span>( <\/span>var1name<\/span>, <\/span>var2name <\/span>)
{
<\/span>temp1 <\/span>= <\/span>VarGet<\/span>( <\/span>var1name <\/span>); <\/span>\/\/ read the value from variable
<\/span>temp2 <\/span>= <\/span>VarGet<\/span>( <\/span>var2name <\/span>);
<\/span>VarSet<\/span>( <\/span>var1name<\/span>, <\/span>temp2 <\/span>); <\/span>\/\/ write the value to variable
<\/span>VarSet<\/span>( <\/span>var2name<\/span>, <\/span>temp1 <\/span>);
}
<\/span>\/\/ Initial values
<\/span>x <\/span>= <\/span>5<\/span>;
<\/span>y <\/span>= <\/span>37<\/span>;
<\/span>\/\/
<\/span>printf<\/span>(<\/span>"Before swap x = %g, y = %g\\\\n"<\/span>, <\/span>x<\/span>, <\/span>y <\/span>);
<\/span>\/\/
<\/span>Swap<\/span>( <\/span>"x"<\/span>, <\/span>"y" <\/span>); <\/span>\/\/ pass names of variables
\/\/
<\/span>printf<\/span>(<\/span>"After swap x = %g, y = %g\\\\n"<\/span>, <\/span>x<\/span>, <\/span>y <\/span>)<\/code>
After swap x = 37, y = 5<\/p><\/blockquote>