473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

variables scope

Hello, All!

I wonder about the correctness of the following simple code:

int func(int ii)
{
ii=100;
return 0;
}

int main(void)
{
int ii=1,jj;
jj=func(ii);
return 0;
}

The point I concern is the variable 'ii'. It's declared in main() as well as
func() argument. Is it considered by standard or I should better avoid such
kinds of code?

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Nov 14 '05 #1
4 993

"Roman Mashak" <mr*@tusur.ru> wrote in message
news:d8***********@relay.tomsk.ru...
Hello, All!

I wonder about the correctness of the following simple code:

int func(int ii)
{
ii=100;
return 0;
}

int main(void)
{
int ii=1,jj;
jj=func(ii);
return 0;
}

The point I concern is the variable 'ii'. It's declared in main() as well
as func() argument. Is it considered by standard or I should better avoid
such kinds of code?


This is fine - no problems as far as the standard goes, stylewise may be
different - but no problems.
Allan
Nov 14 '05 #2
Roman Mashak <mr*@tusur.ru> wrote:
int func(int ii)
{
ii=100;
return 0;
}
If you're expecting the value of ii in main() to be changed from 1 to
100 by this code, you are mistaken. Arguments in C are passed by
value.
int main(void)
{
int ii=1,jj;
jj=func(ii);
return 0;
} The point I concern is the variable 'ii'. It's declared in main() as well as
func() argument. Is it considered by standard or I should better avoid such
kinds of code?


As noted, it's correct.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Fri, 17 Jun 2005 19:49:58 +0900, Roman Mashak wrote:
Hello, All!

I wonder about the correctness of the following simple code:

int func(int ii)
{
ii=100;
return 0;
}

int main(void)
{
int ii=1,jj;
jj=func(ii);
return 0;
}

The point I concern is the variable 'ii'. It's declared in main() as well as
func() argument. Is it considered by standard or I should better avoid such
kinds of code?


This is fine. One of the key features of local function variables
including parameters is that they don't interfere with code outside the
function. So you don't have to worry about name clashes, in fact doing so
might be considered an error.

Lawrence
Nov 14 '05 #4
Roman Mashak wrote:
Hello, All!

I wonder about the correctness of the following simple code:

int func(int ii)
{
ii=100;
return 0;
}

int main(void)
{
int ii=1,jj;
jj=func(ii);
return 0;
}

The point I concern is the variable 'ii'. It's declared in main() as well as
func() argument. Is it considered by standard or I should better avoid such
kinds of code?


The name of a formal parameter of a function has nothing to do with any
object with that name outside of the function.

function (int ii)
{
ii = 100;
return 0;
}

means exactly the same thing as
function (int tickle_my_shoe)
{
tickle_my_shoe = 100;
return 0;
}

whether there are variables elsewhere named either 'ii' or 'tickle_my_shoe'.

For that matter (apart from a possible warning from your compiler if
it's checking for unused arguments), the above are equivalent to

function (int ii)
{
return 0;
}
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hal Vaughan | last post by:
Being self taught, this is one thing I've always had trouble with -- I finally get it straight in one situation and I find I'm not sure about another. I have a class that keeps calling an...
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
7
by: Nicole | last post by:
Hi I'm trying to use a function to set a session variable. I have three files: The first file has: <?php session_start(); // This connects to the existing session ?> <html> <head>
5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
9
by: Stefan Turalski \(stic\) | last post by:
Hi, I done sth like this: for(int i=0; i<10; i++) {...} and after this local declaration of i variable I try to inicialize int i=0;
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
26
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.