Scope modifiers: Difference between revisions

Scope modifiers en FreeBASIC
(Scope modifiers en FreeBASIC)
Line 549:
** exception error: undefined function a_module:add/2
</pre>
 
=={{header|FreeBASIC}}==
* Functions must be defined before being used and are always global in scope.
 
* Variables must be defined before being used.
 
* If a variable is not explicitly defined its scope is local. This may be modified by using the keyword Shared. The effects are detailed by the comments in the sample code.
<lang FreeBASIC>'Declares a integer variable and reserves memory to accommodate it
Dim As Integer baseAge = 10
'Define a variable that has static storage
Static As String person
person = "Amy"
'Declare variables that are both accessible inside and outside procedures
Dim Shared As String friend
friend = "Susan"
Dim Shared As Integer ageDiff = 3
Dim Shared As Integer extraYears = 5
 
Sub test()
'Declares a integer variable and reserves memory to accommodate it
Dim As Integer baseAge = 30
'Define a variable that has static storage
Static As String person
person = "Bob"
'Declare a local variable distinct from a variable with global scope having the same name
Static As Integer extraYears = 2
Print person; " and "; friend; " are"; baseAge; " and"; baseAge + ageDiff + extraYears; " years old."
End Sub
 
test()
Print person; " and "; friend; " are"; baseAge; " and"; baseAge + ageDiff + extraYears; " years old."
Sleep</lang>
{{out}}
<pre>Bob and Susan are 30 and 35 years old.
Amy and Susan are 10 and 18 years old.</pre>
 
=={{header|Free Pascal}}==
2,156

edits