Function Arguments: Know Number w/o Null

  • Thread starter jk22
  • Start date
In summary, function arguments are values that are passed into a function when it is called. The number of function arguments can be determined using the "arguments.length" property within the function. Function arguments can be null, meaning no value is passed in, and it is important to handle them to avoid errors. If more arguments are passed in than expected, the extra ones will be ignored, but it is recommended to handle them in the function. To handle function arguments in code, conditional statements and the "arguments" object can be used to check for specific arguments and perform different actions.
  • #1
jk22
729
24
Is it possible to define a function f (int **a,...)

And to know the number of arguments without writing it in a nor terminating the arguments with Null ?
 
Technology news on Phys.org
  • #2
jk22 said:
Is it possible to define a function f (int **a,...)

And to know the number of arguments without writing it in a nor terminating the arguments with Null ?
As an old-style variadic function, the answer is no.

C99 and C++11 added the concept of variadic macros. I'll leave that up to someone else.

C++11 added the concept of variadic templates. For example,
Code:
#include <iostream>

template<typename... Args>
void print_pointers_to_whatever (Args... args) {
    std::cout << "print_pointers_to_whatever() received "
              << sizeof...(args) << " arguments. Values are:";
    char c[2] = {0,0};
    for (auto p : {args...}) {
        std::cout << c << ' ' << *p;
        c[0] = ',';
    } 
    std::cout << '\n';
}

int main() {
    int i1 = 1, i2 = 2, i3 = 3, i4 = 4;
    double f1 = 1.5, f2 = 2.5, f3 = 3.5;
    const char *hello = "Hello", *world = "world!";

    print_pointers_to_whatever (&i4,&i3,&i2,&i1);
    print_pointers_to_whatever (&f1,&f2,&f3);
    print_pointers_to_whatever (&hello, &world);
}

For a demo of the above, see https://ideone.com/JNe9Qv .
 
Last edited:
  • Like
Likes Silicon Waffle and jk22
  • #4
Svein said:
C has a standard way of dealing with this. See https://en.wikipedia.org/wiki/Stdarg.h.
C does not have a standard way of dealing with this. The article you cited specifically states "There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary."
 
  • Like
Likes Silicon Waffle
  • #5
D H said:
[...]
template<typename... Args>
void print_pointers_to_whatever (Args... args) {
std::cout << "print_pointers_to_whatever() received "
<< sizeof...(args) << " arguments. Values are:";
char c[2] = {0,0};
for (auto p : {args...}) {
std::cout << c << ' ' << *p;
c[0] = ',';
}
std::cout << '\n';
}
[...][/code]
I like C++11/14's latest features but auto key and this new for loop construct I think should be used with care especially when one is having to deal with non-POD or complex user-defined types without user-defined versions of copy ctor, assignment operator, with and without universal references, etc. since their objects will be sliced off or fail to get initialized in the loop.
 
  • #6
D H said:
C does not have a standard way of dealing with this. The article you cited specifically states "There is no mechanism defined for determining the number or types of the unnamed arguments passed to the function. The function is simply required to know or determine this somehow, the means of which vary."
I read it somewhat differently:
  • It provides facilities for stepping through a list of function arguments of unknown number and type.
Of course it cannot specify what it does not know, it just gives you the mechanism to handle a variable number of arguments. The C++ preprocessor uses it all the time.
 
  • Like
Likes Silicon Waffle
  • #7
Silicon Waffle said:
I like C++11/14's latest features but auto key and this new for loop construct I think should be used with care especially when one is having to deal with non-POD or complex user-defined types without user-defined versions of copy ctor, assignment operator, with and without universal references, etc. since their objects will be sliced off or fail to get initialized in the loop.
You can always use for (auto& element : container) in such cases.

That said, the slicing problem is ever present in C++, and it's arguably only worse with constructors, move assignment operators, universal references, etc. Inheritance isn't just a double edged sword, it's sometimes a sword with edges anywhere you look. Sometimes the best way to avoid getting cut is to be very frugal with inheritance. The slicing problem pretty much disappears if you make the constructors and assignment operators in base classes protected, only elevating them to public for the final instantiable classes. In C++11/14 you can declare those final instantiable classes as final.
 

What are function arguments?

Function arguments are values that are passed into a function when it is called. They are used to provide the function with data that it needs to perform its task.

How do I know the number of function arguments?

The number of function arguments can be determined by using the "arguments.length" property within the function. This will return the total number of arguments passed into the function.

Can function arguments be null?

Yes, function arguments can be null. This means that no value is passed into the function for that specific argument. It is important to handle null arguments within the function to avoid errors.

What happens if I pass in more arguments than the function expects?

If more arguments are passed into a function than it expects, the extra arguments will be ignored. However, it is considered good practice to have the function handle unexpected arguments to avoid any potential errors.

How do I handle function arguments in my code?

To handle function arguments in your code, you can use conditional statements and the "arguments" object within the function. This allows you to check for specific arguments and perform different actions based on their presence or value.

Similar threads

  • Programming and Computer Science
Replies
26
Views
2K
  • Calculus and Beyond Homework Help
Replies
8
Views
535
  • Calculus and Beyond Homework Help
Replies
1
Views
407
  • Programming and Computer Science
Replies
4
Views
700
  • Calculus and Beyond Homework Help
Replies
0
Views
408
  • Programming and Computer Science
2
Replies
63
Views
3K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top