A description of function parameters

R

Rob Somers

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?
 
R

Ravi Uday

Rob said:
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
 
E

Eric Sosman

Rob said:
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.
 
P

Peter Nilsson

Rob said:
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;
 
J

Jack Klein

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.)"
 
P

pemo

Jack Klein said:
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.
 
P

pemo

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?
 
R

Richard Bos

pemo said:
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
 
J

Jack Klein

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!
 
R

Rob Somers

Jack Klein wrote:

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
 
A

Amar Prakash Tripaithi

Ya dear,
Obviously, it is. As the scope of the variable within the
function itself.

Amar Prakash Tripathi
 
C

Chris F.A. Johnson

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 -->
 
M

Michael Mair

Rob said:
Jack Klein wrote:




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
 
R

Rob Somers

<snip>

Michael said:
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
 
E

Emmanuel Delahaye

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top