Function: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
JwellsNB (talk | contribs)
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Function ''procedurename''[(''arglist'')] <br />
Function ''procedurename''(''arglist'') <br />
:::[''statements''] <br />
:::[''statements''] <br />
:::[''procedurename'' = ''expression''] <br />
:::[''procedurename'' = ''expression''] <br />
Line 12: Line 12:


Each member of ''arglist'' is an argument passed to the procedure. Simple variables, such as strings and numbers, are passed by value. Complex variables, such as arrays and objects, are passed by reference. Each member of ''arglist'' can be used within the procedure to reference a value passed in. It follows standard variable naming procedure.
Each member of ''arglist'' is an argument passed to the procedure. Simple variables, such as strings and numbers, are passed by value. Complex variables, such as arrays and objects, are passed by reference. Each member of ''arglist'' can be used within the procedure to reference a value passed in. It follows standard variable naming procedure.
To call a function, use the function name followed with parenthesis containing the arguments. If there are no arguments, it is still recommended that parenthesis be supplied, so it is clear that a function is being called.


'''Note:''' If the return value is not stored in a variable or used in an expression, the procedure is called as a Sub procedure, and multiple arguments cannot be enclosed in parenthesis.
'''Note:''' If the return value is not stored in a variable or used in an expression, the procedure is called as a Sub procedure, and multiple arguments cannot be enclosed in parenthesis.

Latest revision as of 15:00, 28 April 2014

Function procedurename(arglist)

[statements]
[procedurename = expression]
[Exit Function]
[statements]
[procedurename = expression]

End Function

Description

Function declares a procedure, procedurename, that executes statements, with arglist as parameters, with an optional return value. The required parameter, procedurename, is used to call the procedure, and must follow standard variable naming conventions. The optional parameter list, arglist, is a comma separated list of variables used to represent variables that are passed to the procedure when it is called. The optional component, statements, will be executed as the body of the procedure. Any number of optional Exit Function statements can be used to exit the procedure during execution. The value returned by the procedure defaults to Empty. To return a value other than the default, assign the value to the function name. The current value of procedurename will be returned when the procedure exits by executing all statements or encountering an Exit Function statement.

Each member of arglist is an argument passed to the procedure. Simple variables, such as strings and numbers, are passed by value. Complex variables, such as arrays and objects, are passed by reference. Each member of arglist can be used within the procedure to reference a value passed in. It follows standard variable naming procedure.

To call a function, use the function name followed with parenthesis containing the arguments. If there are no arguments, it is still recommended that parenthesis be supplied, so it is clear that a function is being called.

Note: If the return value is not stored in a variable or used in an expression, the procedure is called as a Sub procedure, and multiple arguments cannot be enclosed in parenthesis.

Examples (Basic)

Rem Function Example
'Function: a procedure that returns a value
Dim Selection, SalePrice
Selection = Menu("Wednesday")
Print "Wednesday's menu feature:",Selection
SalePrice = Min(31,29)
Print "Sale Price:", SalePrice

Function Menu(day)
  If day = "Wednesday" Then
    Menu = "Salisbury Steak"
  End If
End Function

Function Min(x,y)
  If x > y Then
    Min = y
    Exit Function
  Else
    Min = x
  End If
End Function

Create an object with a function as a member


MyObj = new Object
Function MyObj.myFunction()
  Print "Here I am!"
End Function

'Now call it...
MyObj.myFunction()

Examples (JavaScript)

// Function Example
/* Function: a procedure that returns a value */

var Selection, SalePrice;
Selection = Menu("Wednesday");
NSB.Print("Wednesday's menu feature: " +Selection);
SalePrice = Min(31,29);
NSB.Print("Sale Price: " + SalePrice);

function Menu(day) {
  if(day == "Wednesday") {
     return "Salisbury Steak";
  }
  return '';
}

function Min(x,y) {
  if(x > y) {
     return y;
 } else {
     return x;
  }
}

Create an object with a function as a member


MyObj = new Object;
function MyObj_myFunction() {
  NSB.Print("Here I am!");
}
MyObj.myFunction=MyObj_myFunction;

 //Now call it...
MyObj.myFunction();

Output

Wednesday's menu feature: Salisbury Steak
Sale Price:   29

Related Items

Call, Sub