Jump to content

why doesnt a variable stick around after a function?


kor
 Share

Recommended Posts

$var = "test"
$var2 = "site1"

Select
    Case $var = "test"
        msgbox(0, '', "condition true")
        _Function()
        msgbox(0, '', $newvar)
    Case Else
        ; error
EndSelect

Func _Function()
Select
    Case $var2 = "site1"
        $newvar = "you are at site 1"
    Case $var2 = "site2"
        $newvar = "you are at site 2"
    Case Else
        ; error location
EndSelect
EndFunc

When running this code I get variable used without being declared when trying to get the $newvar output in a msgbox after the function runs inside the loop. What am I doing wrong?

Edited by kor
Link to comment
Share on other sites

It's all about scope.

When you declare a variable in a function it becomes a local scope. When the function ends, the local scope goes away.

Global $varname1
_TestFunction()
ConsoleWrite($varname1)

Func _TestFunction()
   $varname1 = "Test!"
EndFunc

Is an example of a global.

Variable scope is a essential part of any language - you can find a lot of information just going to google and searching. Poor use of Globals/Locals/Functions can result in variables contents "randomly" changing, difficulty debugging, incorrect expected results, and a whole other slew of messes :x

Link to comment
Share on other sites

try just returning the new variable

so at the end of your function put

Return $newvar

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

$var = "test"
$var2 = "site1"
Global $newvar

Select
    Case $var = "test"
        msgbox(0, '', "condition true")
        _Function()
        msgbox(0, '', $newvar)
    Case Else
        ; error
EndSelect

Func _Function()
Select
    Case $var2 = "site1"
        $newvar = "you are at site 1"
    Case $var2 = "site2"
        $newvar = "you are at site 2"
    Case Else
        ; error location
EndSelect
EndFunc

I see. So by declaring $newvar as a global variable it "exists" everywhere within the script? What is the syntax for declaring multiple global vars?

Global $var1 $var2 $var

??? Do I need to separate by commas or anything?

Link to comment
Share on other sites

$var = "test"
$var2 = "site1"
Global $newvar

Select
    Case $var = "test"
        msgbox(0, '', "condition true")
        _Function()
        msgbox(0, '', $newvar)
    Case Else
        ; error
EndSelect

Func _Function()
Select
    Case $var2 = "site1"
        $newvar = "you are at site 1"
    Case $var2 = "site2"
        $newvar = "you are at site 2"
    Case Else
        ; error location
EndSelect
EndFunc

I see. So by declaring $newvar as a global variable it "exists" everywhere within the script? What is the syntax for declaring multiple global vars?

Global $var1 $var2 $var

??? Do I need to separate by commas or anything?

Yea you'd do it with commas

Global $var1, $var2, $var3

There is also the ability to use the local scope and return or "byref" a variable in the local scope

Like what nitekram was talking about above.

$var1 = _TestFunction()
ConsoleWrite($var1)

Func _TestFunction()
    Local $newvar1 = "20"
    Return $newvar1
EndFunc
Link to comment
Share on other sites

An example of using ByRef

$siteID = 10
ConsoleWrite($siteID & @CRLF)
_GetLocationName($siteID)
ConsoleWrite($siteID)

Func _GetLocationName(ByRef $locationID)
    Switch $locationID
        Case 0 To 10
            $locationID = "Home"
        Case 11 To 20
            $locationID = "Work"
        Case Else
            Return 0
    EndSwitch
EndFunc

You can see the function is in local scope ($locationID) but when you modify $locationID, $siteID in the "main", will change to what you declared it to. Which is sorta like what you're looking to do in your example above.

All just basic examples.

Edited by ZacUSNYR
Link to comment
Share on other sites

  • Moderators

kor,

There is a Variables - using Global, Local and ByRef tutorial in the Wiki which you might find useful. :x

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...