Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Return value from sub in CRBasic


Bo Nov 7, 2008 12:42 PM

Hello folks,

I would like to return a value from a subroutine in a CRBasic program. The build in functions is able to do that but i cannot find anything about it in the help.

This is how I want my function to work:

var_1 = my_function(var_2,var_3)

Can somebody tell me that?

Regards,

Bo Holm-Rasmussen


aps Nov 7, 2008 12:59 PM

Up until now CRBasic has not supported functions, just subroutines with parameters. However the very next release of firmware for the CR1000 (and partners) will include support for functions. That OS (Version 16 for the CR1000) will hit the streets any day now and should appear as a download from this website in the next day or so. As a taster here is the help for the new function support (sorry about the formatting):

----

The Function/EndFunction declaration is used to create a user defined function.

Syntax
--------
Function (optional parameters) optional As DataType

Return ( expression ) (optional)

ExitFunction (optional)

EndFunction
---------
Remarks

The Function/EndFunction declaration is similar to a subroutine declaration (Sub/EndSub). By default, a Function is a Float, but it can be specified as a String (with an optional * size), Long, or Boolean. It includes the ability to pass in optional parameters. As with a subroutine declaration, the parameter list describes local parameters and optionally their type (Float, Long, Boolean, String). If not specified, the default parameter type is Float. One difference between a Sub and a Function is a Function returns a value, whereas a subroutine does not.

Function names with their parameters are called just like built in functions; i.e., by simply using their name with parameters anywhere within an expression. When a function is called, the parameters are copied into the Function's local parameter list, as is the case when subroutines are called. However, unlike subroutines, which when exited copy the local parameter values back out to any variables that were passed in, Functions do no such copying back out but rather return a value that is used by the expression that invoked the function.

Return or ExitFunction exits the function at run time. EndFunction terminates the Function declaration and exits the function at run time. The expression returned by the Function is specified by Return(expression),or, if Return is not executed before EndFunction or ExitFunction, then the return value is specified by the assignment of an expression to the Function's name. If neither of these methods are used, then NAN is returned.

Functions can be nested to a maximum of two deep. If a function declaration contains a call to another function, which in turn contains a call to a function, a compiler error is returned.


TweedleDee Nov 21, 2008 06:04 PM

You can pass variables using the Subroutine. The number and sequence of the program variables/values in the call statement must match the number and sequence of the variable list in the sub declaration. Changing the value of one of the variables in this list inside the Subroutine changes the value of the variable passed into it in the calling procedure. So in essence, you can create functions now.

See below for example code.


Public PTemp, batt_volt
Public MyVar1, MyVar2, MyResult
Public Var1, Var2,Result

'Define Data Tables
DataTable (Test,1,-1)
DataInterval (0,1,Sec,10)
Sample (1,MyResult,IEEE4)
Sample (1,MyVar1,IEEE4)
Sample (1,MyVar2,IEEE4)
EndTable

Sub Funct1(Result, Var1, Var2)
Result = Var1 + Var2
EndSub

'Main Program
BeginProg
Scan (1,Sec,0,0)
MyVar1 = MyVar1 + 1
MyVar2 = MyVar2 + 10
Funct1(MyResult, MyVar1, MyVar2)
CallTable Test
NextScan
EndProg


aps Dec 11, 2008 12:39 PM

The new operating systems that includes support for functions are now available from http://www.campbellsci.com/downloads

A

* Last updated by: aps on 12/11/2008 @ 5:39 AM *

Log in or register to post/reply in the forum.