Pic basic compilier

Páginas: 13 (3066 palabras) Publicado: 1 de octubre de 2010
picThe Structured Approach - Using Subroutines and Functions
Subroutines and functions enable you to divide a program into smaller parts. A subroutine or function is a named group of statements, constants, variables and other declarations that perform a particular purpose. A function is identical to a subroutine in every respect, with the one exception: it can return a single value to thecalling program. Swordfish subroutines and functions are non re-entrant, that is, you cannot make recursive subroutine or functions calls.

Parameters
The compilers default parameter passing mechanism is by value or ByVal. Passing by value means that a local copy of the variable is created, and the subroutine or function operates on a copy. If your subroutine or function statement block changes theparameter value, it doesn't change the value of the actual variable being passed. Subroutines and function headings that do not have any formal parameters are written in the following way,
include "USART.bas" sub Print() USART.Write("Hello World", 13, 10) end sub // main code block SetBaudrate(br19200) Print

The subroutine declaration Print() outputs "Hello World" each time it is called. Notethat although no formal parameters have been declared, start and end round brackets are still required. A more useful example would enable any string to be output. To do this, a formal parameter is added,
include "USART.bas" sub Print(pStr as string) USART.Write(pStr, 13, 10) end sub // main code block SetBaudrate(br19200) Print("Hello World")

The Print() subroutine declaration will now outputany string value passed to it. You do not have to explicitly give the size of a formal parameter string when passing a string argument to a subroutine or function. For example, pStr as string(20). This is because Swordfish has a powerful mechanism for calculating at compile time the maximum RAM needed for any string passed by value. In the previous examples, the string parameter argument waspassed to the subroutine using the compilers default mechanism of by value. This is in contrast to passing a

variable by reference or ByRef. Passing by reference means that a subroutine or function receiving the variable can modify the contents of the variable being passed. This is sometimes referred to as a variable parameter. For example,
include "USART.bas" include "Convert.bas" subNoChange(pValue as byte) pValue = 10 end sub sub ChangeValue(byref pValue as byte) pValue = 10 end sub dim Value as byte SetBaudrate(br19200) Value = 0 NoChange(Value) USART.Write("Value : ", DecToStr(Value), 13, 10) ChangeValue(Value) USART.Write("Value : ", DecToStr(Value), 13, 10)

The first subroutine NoChange() has a formal parameter that accepts arguments passed by value. The second subroutineChangeValue() has a formal parameter that accepts arguments passed by reference. When the following lines are executed,
NoChange(Value) USART.Write("Value : ", DecToStr(Value), 13, 10)

The value output will be 0, because NoChange() has received a copy of the contents of Value. When the following lines are executed,
ChangeValue(Value) USART.Write("Value : ", DecToStr(Value), 13, 10)

The valueoutput will now be 10, because ChangeValue() has received the actual RAM address of Value. Some declaration types, such as arrays, must always be passed by reference. For example,
sub PassArray(byref pArray() as byte) end sub

Notice that pArray is followed by open and closing round brackets. This is to inform the compiler that an array is being passed. Without the brackets, the compiler wouldjust interpret the parameter argument as a single byte type. Unlike arrays, structures can be passed by value. However, if your structure has a large number of variables (or uses arrays and strings) it would be more computationally efficient to pass by reference, rather than the compiler having to copy large amounts of data, as would be required if passed by value.

It is important to remember...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Pic basic plus
  • Programación Pic En Basic
  • Secuencia basica PIC
  • Pic en basic
  • Pic basic
  • Pic basic
  • programación de pic basica
  • Pic basic pro castellano

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS