LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 12: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, 12:41 PM   #2
paulsm4
LQ 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, 01:12 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 01: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, 01:53 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Doesn't my previous post answer this already?
 
Old 12-19-2007, 05:14 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 06:10 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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, 07:27 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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



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 03:09 PM
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM
Send variables between functions using pipes. rastadevil Programming 1 03-21-2006 10:26 PM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 10:22 PM
C: setting functions equal to variables LuderForChrist Programming 9 07-14-2004 02:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 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
Open Source Consulting | Domain Registration