LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   variables in functions (https://www.linuxquestions.org/questions/programming-9/variables-in-functions-607898/)

tostay2003 12-19-2007 12:03 PM

variables in functions
 
I was going through a code and found that a variable declared within one function is being used from outside (another function as well).

Is it by default that the variable declared in a function is global.

paulsm4 12-19-2007 12:41 PM

Hi -

I assume you're talking about shell scripts (in the future, it would probably be a good idea to specify the language and cut/paste some example code ... just to give everybody a clearer idea of what you're asking for).

Yes, variables are indeed global in shell scripts (*unlike* most other languages, where variables are usually local by default).

Here's an example:
Code:

:
myfunc () {
  A=letterA
  echo '>>Hello from myfunc!'
  echo ">>A is $A..."
}


echo 'Hello from myScript...'
echo A is $A...
myfunc
echo A is $A...

Quote:

Hello from myScript...
A is ...
>>Hello from myfunc!
>>A is letterA...
A is letterA...
'Hope that helps .. PSM

matthewg42 12-19-2007 01:12 PM

typeset can be used to make the scope of a variable local to a function. Consider the following script:
Code:

#!/bin/bash

testfn () {
        v1="variable1local"
        typeset v2="variable2local"

        echo "in testfn    v1=$v1"
        echo "in testfn    v2=$v2"
}

v1="variable1global"
v2="variable2global"

echo "before testfn v1=$v1"
echo "before testfn v2=$v2"

testfn

echo "after testfn  v1=$v1"
echo "after testfn  v2=$v2"

The output is:
Code:

before testfn v1=variable1global
before testfn v2=variable2global
in testfn    v1=variable1local
in testfn    v2=variable2local
after testfn  v1=variable1local
after testfn  v2=variable2global


tostay2003 12-19-2007 01:33 PM

Hi ,

Thanks for the help. I thought this forum for only unix based questions, so asked my question directly. Next time I will remember to specify the programming language.

I have a follow on question on scripting.. is there a way to force the variable to be local in a function.

Thanks

matthewg42 12-19-2007 01:53 PM

Doesn't my previous post answer this already?

chrism01 12-19-2007 05:14 PM

Actually, at the top of the forum it says
"Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game."
;)
but don't worry about it, you know now.
I think matthew42 has already answered your follow-up qn.

unSpawn 12-19-2007 06:10 PM

Quote:

Originally Posted by matthewg42 (Post 2996013)
typeset can be used to make the scope of a variable local to a function.

Or use 'local'?

matthewg42 12-19-2007 07:27 PM

I think typeset is compatible with ksh which is probably where I picked it up (I don't think local is in ksh). Good to know there's a more readable keyword in bash. Thanks unspawn :D


All times are GMT -5. The time now is 02:16 PM.