Passing Arguments to Procedures

Top  Previous  Next

When calling subroutines or functions, it is often necessary to pass a certain value for processing within the procedure. An example of this would be a function which calculates the sum of two values; naturally, such a function would need to get two arguments -- the values which are to be added up.

There are two different ways to pass arguments to such a procedure:

The Default: Passing By Value

When passing an argument to a procedure by value, this argument is copied to a location in memory which was reserved for the local variables of this function. Processing is then done on this local copy -- the original remains untouched. For example:

 

 

sub foo(x as byte)

...

x = 1

...

end sub

 

sub bar

       dim y as byte

       y = 2

       foo(y)

       ' at this point in code, y is still 2.

end sub

 

 

This way of passing variables is the default used in Tibbo Basic.

Passing By Reference

In certain cases, copying is not the preferred solution; for example, when a procedure has to modify several arguments passed to it and these later have to be accessible. Another example would be when processing large strings -- copying them would cause significant overhead.

In such cases, arguments are passed by reference. When passing by reference, the actual values are not copied. Instead, the procedure receives a reference to the location of the original values in memory -- hence, the name. For example:

 

 

sub foo(byref x as byte)

...

x = 1

...

end sub

 

sub bar

       dim y as byte

       y = 2

       foo(y)

       ' at this point in code, y is 1!

end sub

 

 

When passing arguments by reference, the code within the procedure will access these arguments using indirect addressing. This may cause possible overhead. The only case where it does not cause overhead (relative to passing by value) is when working with large strings; in this case, passing them by reference saves the need to copy the whole string to another location in memory.

note_tip-wt

Here is our advice: when dealing with strings it is usually better (in terms of performance) to pass them by reference. For all other types, passing by value yields better performance.

 

Strict byref argument match is now required!

Beginning with Tibbo Basic release 2.0, strict match is required between the type of byref argument and the type of variable being passed. For example, trying to pass a string for a byte will now cause a compiler error:

 

 

sub bar(byref x as byte)

  ...

end sub

 

sub foo

  bar("123") 'attempt to pass a string will generate a compiler error

  bar(val("123")) 'this will work!

end sub

 

 

In the above example, sub bar takes a byte argument and we are trying to pass a string. Wrong! Compiler understands that byref arguments can be manipulated by the procedure that takes them. Therefore, it is important that actual variables being passed match the type of argument that procedure expects. Automatic type conversion won't apply here.