> In the case that I did have to declare a variable within a function
> with the same name as one that may have been declared in a parent
> document, how would I scope the function variable to only be used in
> the function? How would I refer to it in the function?
>
>
You can prevent the 'declared variable twice' error by using the local
variable struct suggested before:

     <cfset var local = structnew()>
     <cfset local.userid = arguments.userid>

You can also forego all of that and simply refer to arguments.userID
directly, it will, for all intents and purposes be 'local scoped' in the
sense that it is local to the function:

      <cfset currentUser = arguments.userID>


> If I declared userID in a parent document
> And userID is declared in a function that is used n parent document.
> What would <cfset foo = '#userID#"> within that function use? the
> parent userID, or the function local userID?
>
>
This is precisely why local scoping vars is important, it removes this
ambiguity.  The answer is if you have <cfset var userID = 0> in the
function, then refer to later in the function as userID, it will use the
locally scoped one.  With regards to your current issue, you simply want to
refer to it as arguments.userID within the body of the function to remove
any question of which userID it may be referencing.



> Or, in functions, should I create all local function variables as part
> of a structure that is named after the function?
>
> <cfset functionStruct = StructNew()>
>
> <cfset functionStruct.userID = "w/e"
>
> And then within that function, i refer to all function specific
> variables as 'functionStruct.myVariable' rather than just raw
> 'myVariable'. This way, it would guarantee uniqueness among variable
> names?
>
>
Yes, this is what I personally do for the most part.  I admittedly have
never played with the 'always local scope variables' setting in any CF
engine, so I cannot comment on whether it's worthwhile or not.  Finally,
just be sure to include the var declaration if you haven't changed the
admin setting for local scoping:

<cfset var functionStruct = structnew()>

Personally, I think 'local' is more to the point, and at a glance you can
see whether a variable is throwaway or something you intend to return:

<cfset var local = structnew()>
<cfset var returnResult = "">

Etc.

-- 
online documentation: http://openbd.org/manual/
 http://groups.google.com/group/openbd?hl=en

Reply via email to