What is the deeper reason I cannot call a function with named arguments in C/C++ but I can do so in Python?
Question:
In Python, I can call a function with named arguments as below.
def sum (i1,i2):
return (i1 + i2)
print (sum(i1 = 2,i2 = 3))
However in C/C++ I cannot:
#include <iostream>
using namespace std;
int sum(int i1,int i2) { return (i1+i2) ;}
int main()
{
int sum (int i1, int i2);
// cout<< sum (i1 = 2, i2 = 3) << endl; // this becomes a problem
cout<< sum (2, 3) << endl; //this is Okay
return 0;
}
The error returns that "i1, i2 are not declared in this scope".
Is there a deeper reason why this is the case, or it is simply a syntax limitation?
Answers:
It’s just a syntax limitation. There’s no reason behind it aside from "that’s just how the language was designed".
Because syntactically you already can, but it would do not what you expect:
void foo( int i1, int i2 );
int main()
{
int i1 = 0;
int i2 = 0;
foo( i2 = 1, i1 = 2 );
}
So there is no such syntax limitation as you can see here.
They are different languages, and different things work different ways obviously. Otherwise they would be the same language.
C++ language does not support named parameters in regular function calls.
C++ 20 support name parameters on constructors.
You can review some "creative ways" to simulate this feature at the following sites:
What is the deeper reason I cannot call a function with named arguments in C/C++ but I can do so in Python?
There is no reason.1 The C and C++ standards are both voluntary and open to expansion. Simply write your own compiler that supports this feature, and you will have a C and/or C++ implementation that does this.
After that, if you wish, you could promote your new C or C++ variants to other people, rally support, and petition the standards committees to adopt your new feature.
Footnote
1 You will have to modify the grammar somewhat to distinguish sum(i1 = 2, i2 = 3)
that sets the parameters from sum(i1 = 2, i2 = 3)
that evaluates assignments using i1
and i2
in the local scope of the call, and other adjustments may be needed.
The primary reason that C++ doesn’t support named parameter passing is that function arguments don’t necessarily have names. Or the names used in a function declaration can be different than the names used in the definition. Or different declarations can use different names.
For instance:
void foo(int a, int b);
void foo(int b, int a)
{
std::cout << a << ' ' << b;
}
int main()
{
foo(a = 10, b = 20);
}
In the declaration, a
corresponds to the first argument, but in the definition it corresponds to the second parameter. So should the call in main
correspond to foo(10, 20)
or foo(20, 10)
? What if only the declaration is visible? What if the arguments in the declaration don’t have names at all?
Perhaps these sorts of questions could be answered, but it would be quite difficult to answer them in a way that’s consistent across all different possible situations.
In Python, I can call a function with named arguments as below.
def sum (i1,i2):
return (i1 + i2)
print (sum(i1 = 2,i2 = 3))
However in C/C++ I cannot:
#include <iostream>
using namespace std;
int sum(int i1,int i2) { return (i1+i2) ;}
int main()
{
int sum (int i1, int i2);
// cout<< sum (i1 = 2, i2 = 3) << endl; // this becomes a problem
cout<< sum (2, 3) << endl; //this is Okay
return 0;
}
The error returns that "i1, i2 are not declared in this scope".
Is there a deeper reason why this is the case, or it is simply a syntax limitation?
It’s just a syntax limitation. There’s no reason behind it aside from "that’s just how the language was designed".
Because syntactically you already can, but it would do not what you expect:
void foo( int i1, int i2 );
int main()
{
int i1 = 0;
int i2 = 0;
foo( i2 = 1, i1 = 2 );
}
So there is no such syntax limitation as you can see here.
They are different languages, and different things work different ways obviously. Otherwise they would be the same language.
C++ language does not support named parameters in regular function calls.
C++ 20 support name parameters on constructors.
You can review some "creative ways" to simulate this feature at the following sites:
What is the deeper reason I cannot call a function with named arguments in C/C++ but I can do so in Python?
There is no reason.1 The C and C++ standards are both voluntary and open to expansion. Simply write your own compiler that supports this feature, and you will have a C and/or C++ implementation that does this.
After that, if you wish, you could promote your new C or C++ variants to other people, rally support, and petition the standards committees to adopt your new feature.
Footnote
1 You will have to modify the grammar somewhat to distinguish sum(i1 = 2, i2 = 3)
that sets the parameters from sum(i1 = 2, i2 = 3)
that evaluates assignments using i1
and i2
in the local scope of the call, and other adjustments may be needed.
The primary reason that C++ doesn’t support named parameter passing is that function arguments don’t necessarily have names. Or the names used in a function declaration can be different than the names used in the definition. Or different declarations can use different names.
For instance:
void foo(int a, int b);
void foo(int b, int a)
{
std::cout << a << ' ' << b;
}
int main()
{
foo(a = 10, b = 20);
}
In the declaration, a
corresponds to the first argument, but in the definition it corresponds to the second parameter. So should the call in main
correspond to foo(10, 20)
or foo(20, 10)
? What if only the declaration is visible? What if the arguments in the declaration don’t have names at all?
Perhaps these sorts of questions could be answered, but it would be quite difficult to answer them in a way that’s consistent across all different possible situations.