LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 12-19-2007, 01:03 PM   #1
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Rep: Reputation: 15
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.
 
Old 12-19-2007, 01:41 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 12-19-2007, 02:12 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
Old 12-19-2007, 02:33 PM   #4
tostay2003
Member
 
Registered: Jun 2006
Posts: 126

Original Poster
Rep: Reputation: 15
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
 
Old 12-19-2007, 02:53 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Doesn't my previous post answer this already?
 
Old 12-19-2007, 06:14 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
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.
 
Old 12-19-2007, 07:10 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,791
Blog Entries: 54

Rep: Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980Reputation: 2980
Quote:
Originally Posted by matthewg42 View Post
typeset can be used to make the scope of a variable local to a function.
Or use 'local'?
 
Old 12-19-2007, 08:27 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
  


Reply

Tags
function, scope, script, typeset, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
void variables in c functions rubadub Programming 11 10-27-2007 04:09 PM
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 01:01 PM
Send variables between functions using pipes. rastadevil Programming 1 03-21-2006 11:26 PM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 11:22 PM
C: setting functions equal to variables LuderForChrist Programming 9 07-14-2004 03:37 PM


All times are GMT -5. The time now is 01:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration