3

I don't understand why this code is worked.

int f(int,int);

int main()
{
    f(12,21);
    return 0;
}

int f(int,int b)
{
    return 0;
}

How can i use first arg in function f(...) ?

6
  • 6
    Compile with all warnings, don't ignore any.
    – Kerrek SB
    Jan 13, 2012 at 19:14
  • Neither do I. How can you call f before it's being declared? Jan 13, 2012 at 19:15
  • 2
    @ybungalobill: In C, you can. This is called explicit declaration. Perhaps he is talking about C and not C++, or used C compiler to compile this code.
    – user405725
    Jan 13, 2012 at 19:17
  • @ybungalobill: There actually was a declaration, I fixed the formatting so the declaration shows up in the code block. Jan 13, 2012 at 19:18
  • 1
    @VladLazarenko, "implicit" ;-) Jan 13, 2012 at 19:18

6 Answers 6

9

Parameter names are not part of the function signature, only its name and the parameters' types are.

Therefore, it's perfectly legal not naming your parameters. However, you can't use them. (unless of course you do some hacking)

Some hacking: Note - not platform independent, not guaranteed by the standard, but fun :)

void foo (int x, int, int z)
{
   int* pz = &z;
   int y = *(--pz);
   cout << x << " " << y << " " << z;
}

int main()
{
    foo(2,3,4);
    // prints "2 3 4"
}

This works for me on Win7, with MSVS 2008. The code is dependent on how parameters are pushed on the function argument stack.

In production code, you should use names for all parameters, including in declarations, and make them as descriptive as possible.

2
  • 3
    Can you spell UB? Implementation defined doesn't really count, as it could change the next time you compile. (Unless you can point to the public documentation of the compiler that guarantees this particular behaviour as stable)
    – sehe
    Jun 27, 2012 at 11:17
  • My point is claiming 'it works for me' is quite a bit of a stretch already
    – sehe
    Jun 27, 2012 at 11:53
6

You can't. Unless you give it a name. But you don't use any in this function.

Normally, the name of the argument is omitted to avoid warning about it being unused. In the f function you're likely to be warned about b being unused, but not the first argument.

3
  • You can have some fun with C++ though and actually use the parameter :) Jan 13, 2012 at 19:22
  • @LuchianGrigore, sure, if you know the implementation details, but why would you:) Jan 13, 2012 at 19:46
  • @LuchianGrigore, that is a valid reason in my book, I have to admit. Jan 13, 2012 at 20:33
4

you cannot use the first argument of f(...). The missing variable name tells the compiler you explicetly want to ignore this parameter.

Although this might not seem usable at first glance it actually is very much, when you have to follow an api (for example because of virtual methods) and your function simply does not care about everything it get's from the caller.

Simple example:

class Painter {
public:
   virtual void paint( Object o, viewscreen * screen ) = 0;
};

class WindowPainter {
public:
   virtual void paint( Object o, viewscreen * screen ) {
   ...
   }
};

class ConsolePainter {
public:
   // The console is available as std::cout globaly
   // often the name is just put in comments, to reference the common api  
   virtual void paint( Object o, viewscreen * /* screen */) {
   ...
   }
};
2
  • You can if you really want to :). Jan 13, 2012 at 19:26
  • @Luchian: Seen it in your answer... and upvoted for the sheer hackiness of that.
    – LiKao
    Jan 13, 2012 at 19:34
3

You can't use that first argument. The original C language definition was extremely permissive about things like this. Just because you can do it, doesn't mean there's any useful reason to.

1
  • 2
    It's actually very usefull, when the API you have to follow is more generic than you want it to be. For example when you need your function is a virtual method and simply does not need anything that is passed in all cases. Most library use this feature to quite some extent.
    – LiKao
    Jan 13, 2012 at 19:19
2

You cannot use the first argument inside the function and you did not. So this code has worked.

2

Function parameter don't need names if you don't use them.

If you want to use the first argument then give it a name:

int f(int a,int b)
{
    return 0;
}

You rarely see this done in function definitions, but it's more common in declarations.

For instance, if you're "deleting" a constructor then there's no point in naming parameters:

obj& operator=(const obj&) = delete;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.