Copy a string: Difference between revisions

Content deleted Content added
Line 185:
?S$
?C$</lang>
 
==={{header|BaCon}}===
Strings by value or by reference
 
Strings can be stored by value or by reference. By value means that a copy of the original string is stored in a variable. This happens automatically when when a string variable name ends with the '$' symbol.
 
Sometimes it may be necessary to refer to a string by reference. In such a case, simply declare a variable name as STRING but omit the '$' at the end. Such a variable will point to the same memory location as the original string. The following examples should show the difference between by value and by reference.
 
When using string variables by value:
 
<lang freebasic>a$ = "I am here"
b$ = a$
a$ = "Hello world..."
PRINT a$, b$</lang>
 
This will print "Hello world...I am here". The variables point to their individual memory areas so they contain different strings. Now consider the following code:
 
<lang freebasic>a$ = "Hello world..."
LOCAL b TYPE STRING
b = a$
a$ = "Goodbye..."
PRINT a$, b</lang>
 
This will print "Goodbye...Goodbye..." because the variable 'b' points to the same memory area as 'a$'.
 
==={{header|Commodore BASIC}}===