A description of function parameters

Discussion in 'C Programming' started by Rob Somers, Nov 25, 2005.

  1. Rob Somers

    Rob Somers Guest

    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?
    Rob Somers, Nov 25, 2005
    #1
    1. Advertising

  2. Rob Somers

    Ravi Uday Guest

    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
    Ravi Uday, Nov 25, 2005
    #2
    1. Advertising

  3. Rob Somers

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Nov 25, 2005
    #3
  4. 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
    Peter Nilsson, Nov 25, 2005
    #4
  5. Rob Somers

    Jack Klein Guest

    On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
    <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 25, 2005
    #5
  6. Rob Somers

    pemo Guest

    "Jack Klein" <> wrote in message
    news:...
    > On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
    > <> 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.
    pemo, Nov 25, 2005
    #6
  7. Rob Somers

    pemo Guest

    "Eric Sosman" <> wrote in message
    news:...

    <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?
    pemo, Nov 25, 2005
    #7
  8. Rob Somers

    Richard Bos Guest

    "pemo" <> wrote:

    > "Eric Sosman" <> wrote in message
    > news:...
    >
    > > 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
    Richard Bos, Nov 25, 2005
    #8
  9. Rob Somers

    Rob Somers Guest

    Thank you for your replies.

    Rob
    Rob Somers, Nov 25, 2005
    #9
  10. Rob Somers

    Jack Klein Guest

    On Fri, 25 Nov 2005 10:03:19 -0000, "pemo" <>
    wrote in comp.lang.c:

    >
    > "Jack Klein" <> wrote in message
    > news:...
    > > On Thu, 24 Nov 2005 22:23:30 -0500, Rob Somers
    > > <> 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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 25, 2005
    #10
  11. Rob Somers

    Rob Somers Guest

    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
    Rob Somers, Nov 27, 2005
    #11
  12. Ya dear,
    Obviously, it is. As the scope of the variable within the
    function itself.

    Amar Prakash Tripathi
    Amar Prakash Tripaithi, Nov 27, 2005
    #12
  13. 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
    Chris F.A. Johnson, Nov 27, 2005
    #13
  14. Rob Somers

    Michael Mair Guest

    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.
    Michael Mair, Nov 27, 2005
    #14
  15. 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
    Chris F.A. Johnson, Nov 27, 2005
    #15
  16. Rob Somers

    Rob Somers Guest

    <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
    Rob Somers, Nov 27, 2005
    #16
  17. 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
    Emmanuel Delahaye, Nov 27, 2005
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amontec Team
    Replies:
    0
    Views:
    1,775
    Amontec Team
    Jul 11, 2003
  2. Joerg Ritter
    Replies:
    10
    Views:
    2,214
    Marcin
    Dec 6, 2003
  3. Zyd

    VHDL RTL description

    Zyd, Apr 14, 2004, in forum: VHDL
    Replies:
    1
    Views:
    1,478
    H. Li
    Apr 14, 2004
  4. Replies:
    3
    Views:
    314
  5. Jason
    Replies:
    2
    Views:
    504
    Jonathan Mcdougall
    May 13, 2006
Loading...

Share This Page