Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Here's another....file I/O difficulties.
#30
Quote:Well I haven't learned references, pointers, etc. yet. I'll examine that code and see what it means.
Passing a variable by value to a function means that a copy of the variable is made, and that copy is what the function refers to.

Passing a variable by reference means that the object a function recieves is a reference to the original variable. Many compilers implement references as pointers internally, but that should not be relied upon. A reference should be considered as the original variable. Any changes made to a reference affect the original variable. Passing by reference is the default behavior in both QB and FB.

Now, since many compilers implement references as pointers, passing a variable by reference often leads to faster code when passing UDTs and strings because - on my system - a pointer is 4 bytes. UDTs and strings can be potentially quite large and expensive objects to copy. Passing them by reference ensures that these copies are not made, and only a 4-byte pointer is passed instead. You need to be careful when passing by reference - especially since FB doesn't have true constant variables - because again, any changes you make to the reference are made to original variable as well. So, when passing variables that you don't want to be changed (accidentally or otherwise) pass by value, unless the object is a large UDT or string.

In the above code, "option byval" sets the default passing convention to by value. In both functions, "key" is being passed by value, since I don't want to accidentally change it. "text" is being passed by reference because for some reason FB truncates a string being passed to a function if it contains a null (chr$(0)) character but, curiously, this truncation does not happen if the string is copied in code. So, "text" is passed by reference to in order to avoid it being copied internally.
stylin:
Reply


Messages In This Thread
Here's another....file I/O difficulties. - by Anonymous - 01-10-2006, 01:26 PM
Here's another....file I/O difficulties. - by stylin - 01-11-2006, 06:54 PM
Here's another....file I/O difficulties. - by DrV - 01-16-2006, 03:08 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)