Computer Science Canada Unspecified number of parameters for a function? |
| Author: | Naveg [ Wed Oct 12, 2005 4:04 pm ] |
| Post subject: | Unspecified number of parameters for a function? |
Are there any languages that support an unspecified number of parameters for a method? For all I know it may be a concept that exists in every language that I just have not yet reached in my studies. Take for example method lowestCommonMultiple which, as the name suggests, finds the lowest common multiple of given numbers. Now, it would be typical to set the parameters of this method as (int firstNumber, int secondNumber) - at my level anyhow - but what if one wished to find the LCM of more than 2 numbers (ie. an unspecified number of arguments)? Is there a way to do this? I am sure this is possible, and I have a feeling it has to do with concepts I haven't gotten to yet, like recursion, but I'm not sure. If someone could clear this up I would appreciate it. |
|
| Author: | wtd [ Wed Oct 12, 2005 4:09 pm ] | ||
| Post subject: | |||
Perl, Python, and Ruby handle this with remarkable ease.
|
|||
| Author: | Naveg [ Wed Oct 12, 2005 4:11 pm ] |
| Post subject: | |
And what of a language like - your favourite - Java? Or C++ if you'd prefer. Is there any relatively easy way to do this is such languages? |
|
| Author: | wtd [ Wed Oct 12, 2005 4:18 pm ] | ||
| Post subject: | |||
If you're using Java 1.5.0 it is possible. A quick example:
Give me a few minutes to whip up the equivalent C++ code. |
|||
| Author: | wtd [ Wed Oct 12, 2005 4:31 pm ] | ||
| Post subject: | |||
|
|||
| Author: | Monstrosity_ [ Wed Oct 12, 2005 5:06 pm ] |
| Post subject: | |
C and VB also allow you to do this. |
|
| Author: | wtd [ Wed Oct 12, 2005 5:11 pm ] |
| Post subject: | |
"cstdarg" in C++ is simply a wrapper for "stdarg.h", which contains the same macros which work in the exact same manner. |
|