开发者

What are variadic functions in accordance with C and C++?

开发者 https://www.devze.com 2023-01-19 01:46 出处:网络
I am confused. As i asked the question previousl开发者_开发百科y about overloading in C, i got some answers.

I am confused. As i asked the question previousl开发者_开发百科y about overloading in C, i got some answers.

Whenever i try to make others understand about this, I get confused in "variadic functions". Please let me know about it with your shower of knowledge!


In short, they are functions that accept any number of arguments.

http://en.wikipedia.org/wiki/Variadic_function


A variadic function is a (single) function that can be called each time with a different number of arguments. Some variadic functions, like printf, even allow you to use different types of arguments.

The big difference with overloading is that with a variadic function, you have one single function that is designed to handle a different set or arguments for each call. With overloading, you create several different functions, where each function handles a different set of parameters.

One thing that all variadic functions have in common is that there must be a way to tell the function what set of arguments is being passed now. The two common ways are:

  1. One of the first, fixed, parameters describes the variable part. For example, the format string of printf
  2. The last parameter has a special value that indicates 'we are done'. For example, the execl function requires the parameter list to be terminated with a (char*)NULL parameter.


As clearly said so, it used to accept any number of arguments.

Generally, there might be requirements for such method when the user doesn't know how many variables to be passed inside.

Eg: For requirement 1, you require to call the method with 2 parameters. For requirement 2, you require to call the same method with 3 parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消