Printing Formal Parameters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsnayakk
    New Member
    • Apr 2009
    • 1

    Printing Formal Parameters

    #include "stdafx.h"

    void add(int,int)

    {
    printf("Checkin g \n" );

    }


    int _tmain(int argc, _TCHAR* argv[])
    {

    int a=5,b=6;

    add(a,b);

    return 0;

    }

    I was just trying this and I was surprised to see that it compiled with no errors. Someone explained to me that the compiler takes the arguments and assigns them to dummy variable that it (the compiler) creates.
    I would like to know if there is any method to access them ie to print the values of these dummy variables or to do any operation on them.
    Here the dummy variables have the values 5 and 6. i would like to print them. I'm using visual studio 2005 environment.

    thanks


    j.snayak
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by jsnayakk
    #include "stdafx.h"

    I would like to know if there is any method to access them ie to print the values of these dummy variables or to do any operation on them.
    Yes: give them names.
    void add(int, int) tells the compiler what number and types of arguments the function expects. It doesn't care whether you give them names or not; either way the arguments are passed according to the calling convention each time you call the function. By default the compiler generates code that pushes b onto the stack followed by a. The only difference between void add(int, int) and void add(int x, int y) is that your code can't access the arguments unless you fiddle around with asm to access values on the stack directly. Why would you do that?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      If that compiler accepts a type list instead of a formal parameter list in a function definition, feed that compiler to a bunch of hungry scabby cats because it is no good.

      kind regards,

      Jos

      Comment

      Working...