NinjaScript > Educational Resources > Basic Programming Concepts > Functions and Methods Explained |
|
Functions and methods are used to encapsulate a set of statements. They are given a name and optionally a set of input parameters. They can be called from any point in your NinjaScript. Once written, we need only be concerned what the function or method does. In general, the difference between a function and method is that a function returns a value to the calling routine. C# has done away with the term function and only uses the term method. A method can return a value (a function) or return nothing.
Declaring a Method A method must first be declared before you can use it in your script.
[Method Access] [Return Data Type] [Name] (Optional parameters)
Method Access - Sets the access to the method. This topic can be a little complex so lets just use the access level "private" which is all you will ever likely need. Return Data Type - Sets the value type that the method return. If it returns nothing, you will set the return type to "void". Name - A user defined name for the method Optional Parameters - Any optional parameters that you may want to pass into the method
Method With No Return Type
The above method has no return type so we use the keyword "void", we provide a user defined method name PrintDateTime and since there are no parameters we complete the declaration with "()". The method code itself is then enclosed within curly braces.
Method With a Return Type
The above method returns a value of type double so we use the keyword "double", we provide a user defined method name "Multiply" and we declare that this method takes a parameter named "input" which is of value type double. Since this method returns a value, we use the "return" keyword to return the value of (input * 10) back to the calling routine.
Calling a Method within a Method
Building on our examples from above, we added a call to the PrintDateTime() method within our Multiply method.
Using methods is a great way to logically organize blocks of code. If you find that you are performing the same calculations in different parts of your script it also makes sense to encapsulate the repetitive code into a method.
For example:
Lets say you had a script that needed to calculate the average range of the past three bars in multiple locations. This is where encapsulating the logic to that calculates the range into one method comes in handy.
First we declare our method using a return type of double and name it AverageRange. There is no requirement to pass in any parameters. We then write a statement the calculates and returns the average range of the past three bars. We end up with the method below.
We can then reference the AverageRange method anywhere else in our script like the example below shows.
Declaring Variables within a Method You can declare variables within a method. These variables are local in scope which means they can only be accessed within the method and not outside of it such as your main script.
|