Independent sections of code that perform a specific task, and optionally return a value of modifies and argument. They can be called multiple times from within both the Main Scan and SlowSequences. This allows for programs to be simplified as code does not have to be repeated as it is reused whenever required. It also allows for the logical separation and modularisation of a program. Subrotuines must be declared before they are used. They are usually declared just after variable declarations.

Anatomy of a Subroutine


Sub ExampleSubroutine (argument1, argument2, ...) 
...
Lines of code
...
EndSub

Anatomy of a Function


Function Example Function (argument1, argument2, ...) As ReturnType
...
Lines of code
...
Return (ValueToReturn)
EndFunction

Subroutines

Subroutines can take arguments, execute multiple instructions and change the value of its arguments.
Common uses for subroutines include:

  • Data Transfer (FTP & Emails) 
  • Calculations and Calibrations
  • Alarming & Control activation

In the example below this simple subroutine measure a Ponsel pH sensor and returns the value of the three values measured via the subroutine parameters.

 

`-- Measure Ponsel ------------------------------------------------------------------
---
`
***********************************************************************************
****
`
* This subroutine measures the pH using the Ponsel sensor.
*

***********************************************************************************
****
Sub MeasurePonsel(mp_pH, mp_temperature, mp_redox)
Const PONSEL_PORT = 1
Const PONSEL_ADDRESS = 0
Dim mp_ponsel(5)
SDI12Recorder(mp_ponsel,PONSEL_PORT,PONSEL_ADDRESS,”M!”,1,0)
mp_pH = mp_ponsel(2)
mp_temperature = mp_ponsel(1)
mp_redox = mp_ponsel(3)
EndSub

Functions

Functions are very similar to Subroutines but Functions return a value upon conclusion whereas Subroutines do not. This allows for the result of a Function to be used later in the program. Functions are commonly used for processing and/or categorising measurements.


‘ -- Measure DL10 -------------------------------------------------------------------
----
‘ 
***********************************************************************************
****
‘ * This subroutine measures the depth using an EchoPod DL10, returning the depth 
in    *
‘ * meters.                                                                             
*
‘ 
***********************************************************************************
****
Function measureDL10(md_20maDepth,md_4maDepth) As Float
  Dim md_depth

  VoltSe(md_depth,1,mV2500,3,0,0,_50Hz,1,0)
  md_depth = (md_depth - 400) * (md_20maDepth - md_4maDepth) / 1600
  If md_depth < 0 Then md_depth = 0
  If md_depth > md_20maDepth Then md_depth = md_4maDepth

  Return(md_depth)
EndFunction

Subroutine vs Function Implementation

Although you could construction programs to always use subroutines and pass values back via the parameters, its best practice to use functions when you only want to return one value and protect the parameters from inadvertently being modified.

SUBROUTINE FUNCTION

   ‘ -- Measure Depth & calculate discarge
    depth = MeasureDL10(U_20MA_DEPTH,U_04MA_DEPTH)
    If depth >= U_MIN_ACCUMULATING_LEVEL Then
      volume = VolumePassed(depth,MAIN_SCAN_INT,U_PIPE_TYPE)
    Else
      volume = 0
    EndIf


    ‘ -- Measure Depth & calculate discarge
    Call MeasureDL10(depth,U_20MA_DEPTH,U_04MA_DEPTH)
    If depth >= U_MIN_ACCUMULATING_LEVEL Then
      volume = VolumePassed(depth,MAIN_SCAN_INT,U_PIPE_TYPE)
    Else
      volume = 0
    EndIf


 Facebook

We're now on Facebook!

Stay informed with our latest updates by following Campbell Scientific Australia.