views:

381

answers:

4

How do I use function arguments declared like

void f(double)
{
    /**/
}

if it is possible?

+2  A: 

No. You have to give it a name. I.e.

void f(double myDouble)
{
    printf("%f", myDouble * 2);
}

or if you are using iostreams:

void f(double myDouble)
{
    cout << myDouble * 2;
}
Billy ONeal
printf() in c++?
Alan
@Alan: `varargs.h` and the varargs syntax and support.
dmckee
@Alan: The question is equally applicable to C or C++, so I used the C function.
Billy ONeal
@dmckee: I'm referring to: http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.1
Alan
@Alan: I know that, but that doesn't mean others won't read this and find it applicable elsewhere.
Billy ONeal
@Alan: Answer edited to acknowledge iostreams.
Billy ONeal
There's nothing wrong with using printf in c++.
Alan
@Billy: Use `%f` or `%g` to printf a double.
KennyTM
@KennyTM: Thank you :)
Billy ONeal
+20  A: 

I hope an example can provide some help:

// Declaration, saying there is a function f accepting a double.
void f(double);

// Declaration, saying there is a function g accepting a double.
void g(double);

// ... possibly other code making use of g() ... 

// Implementation using the parameter - this is the "normal" way to use it. In
// the function the parameter is used and thus must be given a name to be able
// to reference it. This is still the same function g(double) that was declared
// above. The name of the variable is not part of the function signature.
void g(double d)
{
  // This call is possible, thanks to the declaration above, even though
  // the function definition is further down.
  f(d);
}

// Function having the f(double) signature, which does not make use of 
// its parameter. If the parameter had a name, it would give an 
// "unused variable" compiler warning.
void f(double)
{
  cout << "Not implemented yet.\n";
}
Anders Abel
This is the best answer here. +1.
Billy ONeal
I agree, +1 from me
Carson Myers
OK, it works good.Sorry for my english.
zed91
@zed91 - You should mark the answer that helped you solve the problem as "accepted". You can mark any answer as accepted, regardless of the voting score. It is considered polite to always accept an answer for your questions. It will also get you +2 in reputation and the author of the answer +15.
Anders Abel
+1  A: 

The parameter may still get placed onto the stack, so you may be able to find it there (see comments below)

For example only (highly non-portable)

#include<stdio.h>
void f(double)
{
    double dummy;
    printf("%lf\n",*(&dummy-2)); //offset of -2 works for *my* compiler
}

int main()
{
    f(3.0);
}

I'm not sure why you'd want to do this though

gnibbler
This is highly implementation-dependent (depends on CPU, ABI, etc).
Paul R
@Paul R. It's my interpretation of the question. At least until the OP clarifies it. I added a note to my answer
gnibbler
@gnibbler - noted, and -1 removed. Suggest you also `s/will/may/` and `s/can/may be able to/`, as there are plenty of ABIs which use registers for passing parameters.
Paul R
Arguments aren't necessarily passed on the stack. More likely to be passed in a register, for example if using SSE2 FP (quite likely).
Potatoswatter
I don't see the value in this at all. There is really nothing we can tell the OP about where to find that data. If he has the ability to search for it, he has the ability to give the argument a name.
Potatoswatter
A: 

Compiler will pass 0 by default....its the way we used to distinguish postfix increment operator and we never have to use the actual value passed..

Guru