Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A description of function parameters

Reply
Thread Tools

A description of function parameters

 
 
Rob Somers
Guest
Posts: n/a
 
      11-25-2005
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?

 
Reply With Quote
 
 
 
 
Ravi Uday
Guest
Posts: n/a
 
      11-25-2005


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

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-25-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid

 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-25-2005
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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-25-2005
On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
<(E-Mail Removed)> 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
 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      11-25-2005

"Jack Klein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
> <(E-Mail Removed)> 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.




 
Reply With Quote
 
pemo
Guest
Posts: n/a
 
      11-25-2005

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..

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


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      11-25-2005
"pemo" <(E-Mail Removed)> wrote:

> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>
> > 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
 
Reply With Quote
 
Rob Somers
Guest
Posts: n/a
 
      11-25-2005
Thank you for your replies.

Rob
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      11-25-2005
On Fri, 25 Nov 2005 10:03:19 -0000, "pemo" <(E-Mail Removed)>
wrote in comp.lang.c:

>
> "Jack Klein" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
> > <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class Member Data and Member Function Parameters - Should Parameters Be Data Members? Jason C++ 2 05-13-2006 07:11 AM
what is the most effortless way to add a description comment to a function bluekite2000@gmail.com C++ 3 08-11-2005 10:14 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Are default function parameters in allowed in function templates? BRG C++ 11 01-04-2005 02:48 PM
restriction of a two-parameters function to a one-parameter function Boris Sargos C++ 3 04-23-2004 04:14 PM



Advertisments