Jump to content

Local and Global variables help


Recommended Posts

Problem:

I need a function that will accept data from another function but I don't want the variable to be accessible to the main program (hopefully that makes sense).

What I have is this:

Local $c
Main Body here
Call to myFunction

Func myFunction()
    do some stuff here
     While
         do some other stuff
      Wend
    myInc(1)
    do somemore stuff
EndFunc

Func myInc($c)
   $r = $r + (15 * $c)
EndFunc

What I have works fine. What's the proper way to do it though? If I just call

myInc()

and don't supply a value it fails. So I'm missing error checking I guess.

And I just noticed the help file concerning Local. Should I be setting Local variables within the function that they're in or can I set them in the main body of the script?

Thanks!

Raoul S. Duke: Few people understand the psychology of dealing with a highway traffic cop. Your normal speeder will panic and immediately pull over to the side. This is wrong. It arouses contempt in the cop-heart. Make the bastard chase you. He will follow.
Link to comment
Share on other sites

  • Developers

This example will return a value into a local var $y :

Local $C
; Main Body here
myFunction()
Func myFunction()
   Local $y
  ;do some stuff here
   While
     ;do some other stuff
   Wend
   $y = myInc(1)
  ; do somemore stuff
EndFunc  ;==>myFunction

Func myInc($C)
   $R = $R + (15 * $C)
   Return $r
EndFunc  ;==>myInc

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Local $C
; Main Body here
myFunction()
Func myFunction()
   Local $y
 ;do some stuff here
   While
    ;do some other stuff
   Wend
   $y = myInc(1)
 ; do somemore stuff
EndFunc ;==>myFunction

Func myInc($C)
   $R = $R + (15 * $C)
   Return $r
EndFunc ;==>myInc
In the line

$y=myInc(1)

if you don't supply a value (the 1) does your function error out? Mine does. Is that OK? I figured that it was wrong and you should be able to do this:

$y=myInc()

with no value and not have it error out. But I might just be ignorant. Mine fails because the $c isn't declared anywhere so it tries to multiply with a no value and promptly stops. I can understand that. I thought there might be some proper programatic way to verify a value and then continue the function. I mean, I know I can check $c <> no value and continue else fail or something.

I gotta stop checking this damn board. I might miss a deadline or something. :ph34r:

I'm just tyring to learn how to code properly/nicely/uniformly. I've already picked up a couple of new things that I'm trying to develop into habits.

Hungarian Notation

Ending functions with ; ==> FunctionNameHere

And lot's more comments :(

My other coding habits are just bad.

Raoul S. Duke: Few people understand the psychology of dealing with a highway traffic cop. Your normal speeder will panic and immediately pull over to the side. This is wrong. It arouses contempt in the cop-heart. Make the bastard chase you. He will follow.
Link to comment
Share on other sites

  • Developers

In the line

$y=myInc(1)

if you don't supply a value (the 1) does your function error out?  Mine does.  Is that OK?  I figured that it was wrong and you should be able to do this:

$y=myInc()

with no value and not have it error out.  But I might just be ignorant.  Mine fails because the $c isn't declared anywhere so it tries to multiply with a no value and promptly stops.  I can understand that.  I thought there might be some proper programatic way to verify a value and then continue the function.  I mean, I know I can check $c <> no value and continue else fail or something.

It is not possible to have optional parameters on Functions... there have been several discussion on this item.. so just do a search on the alternatives.

Hungarian Notation

Ending functions with ; ==> FunctionNameHere

And lot's more comments :ph34r:

My other coding habits are just bad.

The "; ==> FunctionNameHere" bit is generated by Tidy, which i always run for proper indenting....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Oh, I didn't know that Tidy put those in. It looks good to me anyway. I've already started using it. :ph34r:

As far as the optional parameters being passed to Functions I hadn't seen any posts regarding that. I'll start searching now.

Thanks!

Raoul S. Duke: Few people understand the psychology of dealing with a highway traffic cop. Your normal speeder will panic and immediately pull over to the side. This is wrong. It arouses contempt in the cop-heart. Make the bastard chase you. He will follow.
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...