473,385 Members | 1,492 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,385 software developers and data experts.

A description of function parameters

Say I have the following code:

void foo(int some_int);
.....
int x = 5;
foo(x);
.....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?

Nov 25 '05 #1
16 1631


Rob Somers wrote:
Say I have the following code:

void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?

Yes some_int will be considered local and its better if it is initializ\
ed externally. Further, any changes you make to that variable
inside the function doesnt hold once you exit out of that function.
If you need to manupalate the value and have it saved upon exit
you can pass it as a pointer !
Get a C-book fassst :)

- Ravi

Nov 25 '05 #2
Rob Somers wrote:
Say I have the following code:
All right: "I have the following code."
void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?


The identifier `some_int' appears in two contexts: once
in the declaration of foo() and once in its definition. In
the declaration it's really just commentary: A name that
never gets used and could just as well have been omitted or
replaced with another. You could have written the declaration
as `void foo(int);' or as `void foo(int huge_array_of_float);'
with exactly the same effect.

In the definition, `some_int' is the name of the function's
formal parameter. The parameter behaves very much like a local
variable that has been initialized with the value of the
corresponding argument expression provided by the caller. The
parameter exists and has its value when foo() starts to execute,
and it continues to exist until foo() returns or otherwise ceases
to execute. Like a local variable it retains its value until and
unless you assign a new one to it, and then retains that value.
Technically, though, it is not a local variable but a parameter --
but this is a tautology, because technically there is no such
thing as a "local variable" in the languge.

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 25 '05 #3
Rob Somers wrote:
Say I have the following code:

void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?


The standards talk in terms of storage duration and identifier scope.

Here, some_int has automatic duration and function prototype scope.

When foo(x) is called, some_int is assigned x, as in: some_int = x;

--
Peter

Nov 25 '05 #4
On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
<mr**********@hotmail.com> wrote in comp.lang.c:
Say I have the following code:

void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?


These two paragraphs from section "6.9.1 Function definitions" of the
C standard should answer your questions:

"9 Each parameter has automatic storage duration. Its identifier is an
lvalue, which is in effect declared at the head of the compound
statement that constitutes the function body (and therefore cannot be
redeclared in the function body except in an enclosed block). The
layout of the storage for parameters is unspecified."

"10 On entry to the function, the size expressions of each variably
modified parameter are evaluated and the value of each argument
expression is converted to the type of the corresponding parameter as
if by assignment. (Array expressions and function designators as
arguments were converted to pointers before the call.)"

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 25 '05 #5

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:n7********************************@4ax.com...
On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
<mr**********@hotmail.com> wrote in comp.lang.c:
Say I have the following code:

void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?


These two paragraphs from section "6.9.1 Function definitions" of the
C standard should answer your questions:


If the declarator includes an identifier list,

each declaration in the declaration list shall have at least one declarator,

those declarators shall declare only identifiers from the identifier list,

and every identifier in the identifier list shall be declared.

Then, shalt thou count to three, no more, no less.
Three shalt be the number thou shalt count, and the number of the
counting shalt be three. Four shalt thou not count, nor either
count thou two, excepting that thou then proceed to three. Five is
right out.


Nov 25 '05 #6

"Eric Sosman" <es*****@acm-dot-org.invalid> wrote in message
news:fr******************************@comcast.com. ..

<snip>
Technically, though, it is not a local variable but a parameter --
but this is a tautology, because technically there is no such
thing as a "local variable" in the languge.


What's the proper term for a 'local variable' then please?
Nov 25 '05 #7
"pemo" <us***********@gmail.com> wrote:
"Eric Sosman" <es*****@acm-dot-org.invalid> wrote in message
news:fr******************************@comcast.com. ..
Technically, though, it is not a local variable but a parameter --
but this is a tautology, because technically there is no such
thing as a "local variable" in the languge.


What's the proper term for a 'local variable' then please?


There are two aspects (at least) to "local", so it depends on whether
you're talking about where the identifier is visible or for how long the
object exists. In the first case, it's a "block scope identifier"; in
the latter, an "object with automatic storage duration".

A function parameter is very similar to what is usually called a "local
variable": both have block scope (yes, the parameter as well), and use
automatic storage. The different one is the parameter in a function
_prototype_ that isn't also part of a function definition (i.e., an
early or external declaration prototype); that has function prototype
scope, and no storage (since it doesn't actually refer to an object
yet).

I don't think you can get an automatic object designated by an
identifier with anything but block scope, but the other way 'round is
possible: a "local variable" declared with the static keyword has block
scope, but the object has static duration.

Richard
Nov 25 '05 #8
Thank you for your replies.

Rob
Nov 25 '05 #9
On Fri, 25 Nov 2005 10:03:19 -0000, "pemo" <us***********@gmail.com>
wrote in comp.lang.c:

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:n7********************************@4ax.com...
On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
<mr**********@hotmail.com> wrote in comp.lang.c:
Say I have the following code:

void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)? If not, what would it be considered as?


These two paragraphs from section "6.9.1 Function definitions" of the
C standard should answer your questions:


If the declarator includes an identifier list,

each declaration in the declaration list shall have at least one declarator,

those declarators shall declare only identifiers from the identifier list,

and every identifier in the identifier list shall be declared.

Then, shalt thou count to three, no more, no less.
Three shalt be the number thou shalt count, and the number of the
counting shalt be three. Four shalt thou not count, nor either
count thou two, excepting that thou then proceed to three. Five is
right out.


Silence, dolt, lest I let loose the Holy Hand Grenade of Antioch!

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 25 '05 #10
Jack Klein wrote:

<snip>
These two paragraphs from section "6.9.1 Function definitions" of the
C standard should answer your questions:

"9 Each parameter has automatic storage duration. Its identifier is an
lvalue, which is in effect declared at the head of the compound
statement that constitutes the function body (and therefore cannot be
redeclared in the function body except in an enclosed block). The
layout of the storage for parameters is unspecified."

"10 On entry to the function, the size expressions of each variably
modified parameter are evaluated and the value of each argument
expression is converted to the type of the corresponding parameter as
if by assignment. (Array expressions and function designators as
arguments were converted to pointers before the call.)"


Hey Jack, that is very helpful for me. One thing more (for now anyway) that
I would like to ask. In section 9 it seems to state (unless I am
misunderstanding) that an identifier with the same name as a parameter
cannot be redclared, *unless* it is within a block, and therefore is hidden
in that block. Now I am cross referencing with Harbinson and Steele (5th
edition) and here is what they say:

"In standard C, formal parameters have the same scope as identifiers
declared at the top level of the function body, and therefore they cannot
be hidden or redeclared by declarations in the body. Some current C
implementations allow such a redeclaration, which is almost invariably a
programming error." (C A Reference Manual, 5th edition, pg 295 Section
9.3)
Now what that looks like to me, is a contradiction to what the standard
says. Am I understanding this correctly?

Rob Somers
Nov 27 '05 #11
Ya dear,
Obviously, it is. As the scope of the variable within the
function itself.

Amar Prakash Tripathi

Nov 27 '05 #12
On 2005-11-25, pemo wrote:

If the declarator includes an identifier list,

each declaration in the declaration list shall have at least one declarator,

those declarators shall declare only identifiers from the identifier list,

and every identifier in the identifier list shall be declared.

Then, shalt thou count to three, no more, no less.
Three shalt be the number thou shalt count, and the number of the
counting shalt be three. Four shalt thou not count, nor either
count thou two, excepting that thou then proceed to three. Five is
right out.


comp.lang.python is thataway -->

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Nov 27 '05 #13
Rob Somers wrote:
Jack Klein wrote:

<snip>
These two paragraphs from section "6.9.1 Function definitions" of the
C standard should answer your questions:

"9 Each parameter has automatic storage duration. Its identifier is an
lvalue, which is in effect declared at the head of the compound
statement that constitutes the function body (and therefore cannot be
redeclared in the function body except in an enclosed block). The
layout of the storage for parameters is unspecified."

"10 On entry to the function, the size expressions of each variably
modified parameter are evaluated and the value of each argument
expression is converted to the type of the corresponding parameter as
if by assignment. (Array expressions and function designators as
arguments were converted to pointers before the call.)"

Hey Jack, that is very helpful for me. One thing more (for now anyway) that
I would like to ask. In section 9 it seems to state (unless I am
misunderstanding) that an identifier with the same name as a parameter
cannot be redclared, *unless* it is within a block, and therefore is hidden
in that block. Now I am cross referencing with Harbinson and Steele (5th
edition) and here is what they say:

"In standard C, formal parameters have the same scope as identifiers
declared at the top level of the function body, and therefore they cannot
be hidden or redeclared by declarations in the body. Some current C
implementations allow such a redeclaration, which is almost invariably a
programming error." (C A Reference Manual, 5th edition, pg 295 Section
9.3)
Now what that looks like to me, is a contradiction to what the standard
says. Am I understanding this correctly?


Not at all.
H&S tell you to treat the body of
void myhappyfunfunction (long someparam)
{
....
{ /* inner block */
}
}
as if it read
long someparam;
....
{ /* inner block */
}
rather than
long someparam;
{....
{ /* inner block */
}
}
This does not hinder you to declare something with identifier
someparam in the inner block -- which is exactly the same as
the standard says. The sentence making you doubt just tells you
that
void myhappyfunfunction (long someparam)
{
unsigned char someparam;
....
{ /* inner block */
}
}
is forbidden. It does not touch the semantics of compound
statements (blocks) which allow zero or more declarations
before zero or more statements. That is,
void myhappyfunfunction (long someparam)
{
....
{ /* inner block */
unsigned char someparam;
}
}
_is_ allowed.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 27 '05 #14
On 2005-11-27, Amar Prakash Tripaithi wrote:
Ya dear,
Obviously, it is.
What is obviously what? Please read:

<http://cfaj.freeshell.org/google>
As the scope of the variable within the
function itself.

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Nov 27 '05 #15
<snip>

Michael Mair wrote:
Not at all.
H&S tell you to treat the body of
void myhappyfunfunction (long someparam)
{
....
{ /* inner block */
}
}
as if it read
long someparam;
....
{ /* inner block */
}
rather than
long someparam;
{....
{ /* inner block */
}
}
This does not hinder you to declare something with identifier
someparam in the inner block -- which is exactly the same as
the standard says. The sentence making you doubt just tells you
that
void myhappyfunfunction (long someparam)
{
unsigned char someparam;
....
{ /* inner block */
}
}
is forbidden. It does not touch the semantics of compound
statements (blocks) which allow zero or more declarations
before zero or more statements. That is,
void myhappyfunfunction (long someparam)
{
....
{ /* inner block */
unsigned char someparam;
}
}
_is_ allowed.
Cheers
Michael


Ok great - thanks for the explanation - after reading it and looking again
at H&S it seems to make sense now.

Rob
Nov 27 '05 #16
Rob Somers a écrit :
Say I have the following code:

void foo(int some_int);
....
int x = 5;
foo(x);
....

void foo(int some_int)
{
printf("%d\n", some_int);
}

Would 'some_int' be considered a local variable, (which happens to be
initialised externally)?


Yes.

--
A+

Emmanuel Delahaye
Nov 27 '05 #17

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

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
1
by: John Miles | last post by:
Hi -- This is a bit of an implementation-specific problem, but I'd like to post it here to see if there's a general answer within the auspices of the language. I'm developing a high(er)-level...
2
by: Dennis Ruppert | last post by:
I created a routine to read and edit the description properties of tables, (the one you see in the database window). It works just fine. This is the basic code behind it, I substituted all my...
6
by: Sergey Poberezovskiy | last post by:
Hi, I have already asked the question, but probably did not make myself clear. Back in VB6 days when you created a public method/property/event you could define what's called "Procedure...
6
by: Amin Sobati | last post by:
Hi, I want to create description for parameters of subs or functions like what VB.NET does (e.g when you are typing a function, a windows like a big tooltip displays various overloaded versions of...
11
by: Yelena Varshal via AccessMonster.com | last post by:
Hello, I have a problem with one of msaccess.exe API calls that work on my desctop but does not work on the laptop from within MS ACCESS. There is a lot of differences between 2 computers...
2
by: Maxwell_Smart | last post by:
Is there a way for a function to refer to itself generically? I'd like to use such a thing (should it exist) for convenience and consistency, not functionality. For example: Function...
4
by: Quinn | last post by:
If I have a procedure like Sub SUB1 (i as integer, s as string) end sub how do I write some descriptions for i and s? that way when I call Sub1 there will be a pop toothtip to describe what...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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.