Can Public Functions Access All Program Variables?

New to FreeBASIC? Post your questions here.
fxm
Moderator
Posts: 11219
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can Public Functions Access All Program Variables?

Post by fxm »

caseih wrote: Feb 15, 2022 5:07 Sure but you can shadow any global variable you want in a function or sub. Absolutely no problem there.
Only since the bugs relating to looking-up duplicate variable names seem to be corrected now in the last FB version 1.09.0 !
(otherwise, big problems before)
exagonx
Posts: 255
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Can Public Functions Access All Program Variables?

Post by exagonx »

Mike Green9 wrote: Feb 09, 2022 18:46 FreeBasic 1.07 Ubuntu 20.04

Hi.

Here is a sample program containing a Function GetWeight(). Is there anyway that a Function can access ALL program Variables such as Var1, Var2, and String1 in this example ?

Code: Select all

Dim As Integer Var1, Var2, Weight, W
Dim As String String1, String2

Public Function GetWeight() As Integer
    Dim As Integer Weight = 20
    Var1 = Weight * Var2                    'Defined Outside Of This Function
    String1 = "Changed in Function"    ''Defined Outside Of This Function
    Return  Weight
End Function

'   Program Begins
    Var1 = 87 :    Var2 = 12  : Weight = 11
    String1 = "String1" :  String2 = "String2"

    W = GetWeight
    Print W, Var1, Var2, String1, String2
I would also ask the same question about Sub/End Sub.

Thanks,
M....
Hi, the simplest way to share variables within a SUB or FUNCTION is to use global variables with: DIM SHARED myvar AS "TYPE"
Ill make you an example:

Code: Select all

    
    Dim shared as integer Var1 , Var2  
    Dim shared as string String1
    
Function GetWeight() As Integer
    Dim As Integer Weight = 20
    Var1 = Weight * Var2                    'Defined Outside Of This Function
    String1 = "Changed in Function"    ''Defined Outside Of This Function
    Return  Weight
End Function

'   Program Begins
    dim as integer Weight = 20 
    Var1 = 87 :    Var2 = 12 
    String1 = "String1" :  String2 = "String2"

    W = GetWeight(Weight)
    Print W, Var1, Var2, String1, String2

In your code you used the PUBLIC prefix this works in the classes to make a FUNCTION, Variable, Array or SUB Public, Private, Protected.

Code: Select all

type myclass
    	Dim as integer Var1 , Var2  
	Dim as string String1, String2
    Public:
    	Declare Function GetWeight(byval Weight as integer ) As Integer
    Private:
    	dim as integer privatevar = 10 ' this var can be used only by member of this class
    	
end type
	 
Function myclass.GetWeight(byval Weight as integer) As Integer ' This function preceded by the name of the class. becomes an effective member of the class
    
    this.Var1 = Weight * this.Var2          'Defined Outside Of This Function
    this.String1 = "Changed in Function"    'Defined Outside Of This Function
    'this.privatevar you can use here 
    Return  Weight
End Function

'   Program Begins
    DIM example as myclass 'We create an object by defining it as a class
    dim as integer Weight = 20 
    dim as integer W = 0 ' You have to define every var or array you need to be used
    
    example.Var1 = 87 :   example.Var2 = 12  
    example.String1 = "String1" :  example.String2 = "String2"

    W = example.GetWeight(Weight) 'Weight is not a public or shared variable therefore if it is to be read within the function it must be specified in the function itself. (byval myvar as type)
    Print W, example.Var1, example.Var2, example.String1, example.String2
	'example.privatevar you cannot use here because is private
The beauty of using type is that each component can be initialized as many times as needed without losing its content.
Parkin
Posts: 2
Joined: Mar 24, 2022 10:26

Re: Can Public Functions Access All Program Variables?

Post by Parkin »

Aren't all the variables in a namespace shared? Then you could have a namespace called global and access variables like global.x and global.y
exagonx
Posts: 255
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Can Public Functions Access All Program Variables?

Post by exagonx »

Parkin wrote: Jun 17, 2022 14:55 Aren't all the variables in a namespace shared? Then you could have a namespace called global and access variables like global.x and global.y
Nope

Code: Select all

dim shared as integer SharedVar = 1
dim as integer NoSharedVar = 1

sub mysub()
	print SharedVar 'show 1'
	print NoSharedVar 'error
end sub

	print SharedVar 'show 1'
	print NoSharedVar 'show 1'
mysub()

In FreeBASIC if you want set a var to use inside function or object you have to put in shared, or that can be used only in the level code where was initializated.
fxm
Moderator
Posts: 11219
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Can Public Functions Access All Program Variables?

Post by fxm »

@Parkin only says that variables declared in a namespace are global (shared) by default:

Code: Select all

namespace global
    dim as integer SharedVar = 1
end namespace

dim as integer NoSharedVar = 1

sub mysub()
	print global.SharedVar 'show 1'
	print NoSharedVar 'error
end sub

	print global.SharedVar 'show 1'
	print NoSharedVar 'show 1'
mysub()
Lost Zergling
Posts: 522
Joined: Dec 02, 2011 22:51
Location: France

Re: Can Public Functions Access All Program Variables?

Post by Lost Zergling »

Usually most skilled programmers will consider as a good practise to use as less global declaration as possible, nevertheless :
- This shall not be an absolute rule, using global (shared) sometimes relevant
- Will depend also the coding style
I would recommend you practise coding avoiding global as much as possible, only using it as a 'fail safe' mode as far as you hardly see better solution.
Using then shared later will usually be easier than learning afterwards how doing without.
It might be a little bit harder, but I think you won't regret it.
exagonx
Posts: 255
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Can Public Functions Access All Program Variables?

Post by exagonx »

Lost Zergling wrote: Jun 18, 2022 0:15 Usually most skilled programmers will consider as a good practise to use as less global declaration as possible, nevertheless :
Sometime you have no choice:
Like this example.

Code: Select all

dim shared CurrentUser as string

sub SetUser()
	CurrentUser = "USER"
end sub
sub testA()
	print "The current user:" & CurrentUser
end sub
Sub Directory()
	open for input CurrentUser & "\mysave.sav" as #1
	close #1
end sub

Global Declaration exist because some time you have to use it

skilled programmer know how and when have to use a global because find a problem when you have a 1000 rows of line code become hard with many globals that becuase is better use it only when the other way are to complicated.

this is my thought, please if you have another idea explain it
Lost Zergling
Posts: 522
Joined: Dec 02, 2011 22:51
Location: France

Re: Can Public Functions Access All Program Variables?

Post by Lost Zergling »

Using as few global declarations as possible introduce a refined distinction between scope and persistency.
'Shared' means global scope and persistency. 'Static' means persistency. Technically the difference is close to zero (stored in the 'heap' because persistency is implied by global), BUT, conceptually, the distinction between the memory cartography and the persistency (dynamic) requirements has some importance.
Afterwards, managing a 'dynamic persistency' can be performed (as soon as I'm not mistaken) using (functions, etc) scopes and object programming one hand, or manual allocations/deallocations other hand. Even though there is not an explicit dynamic memory management specified by programmer lots of global use cases are rarelly full global requirements and can usually be turned static with little algorithmic change.
Then, it shall be easier to reduce the gap when it'll come to move to a managed dynamic memory management.
ps : I considered using 'dim shared gCollector as list' in lzle was a 'relevant' use case, while being nevertheless never an ideal solution !
exagonx
Posts: 255
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Can Public Functions Access All Program Variables?

Post by exagonx »

Lost Zergling wrote: Jun 18, 2022 12:08 Using
Yes you are right, use a Global like a static its useless and can cause some troubles , some time you have to change data inside a global and then istead static you ave to use a global var.

I use TYPE, SCOPE, too and global become useless with pointers and statics you have all under control and its easy to find the error in a large ammount of code.
Lost Zergling
Posts: 522
Joined: Dec 02, 2011 22:51
Location: France

Re: Can Public Functions Access All Program Variables?

Post by Lost Zergling »

Indeed.
Post Reply