0

In python we can pass arguments to called function using argument name like in the following code , how can we do the same thing C++ , I needed this because ,if we have too many parameter it easy to mess up the location of the argument passed in called function and calling function

def calendar(year,month,day):
    return "f{day}:{month}:{year}"
print(calendar(day=4,year=2021,month=9) #here arguments are passed in order of day,year and month  

Output "4:9:2021" irrespective of passing parameters not in order as passed while defining function by using named arguments

string calendar(int year,int month,int day)
{
    return year,month,day;
}

int main ()
{       
cout<<calendar(" ? ");// How can we pass arguments like that of python in c++
}

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 4
    Where exactly are you stuck? Also, do not ask questions about "C or C++". They are different languages, and if you want to know about both then you have two questions. The way that you ask this suggests that you don't actually have a problem that needs solving (or you would have picked an implementation language and given adequate context for the problem) but are just looking to compare languages as a sort of trivia. Please keep in mind that Stack Overflow is *not a discussion forum*. – Karl Knechtel Sep 04 '21 at 18:33
  • In c++ , actually I wanna pass parameter values along with parameter name , by that we dont have to remember the position of parameter , – Puneeth G P Sep 04 '21 at 18:37
  • 4
    Neither C nor C++ support named arguments. So you cannot do that. The closest you can get to named arguments in C++ are hacks like this: https://pdimov.github.io/blog/2020/09/07/named-parameters-in-c20/ – Nikos C. Sep 04 '21 at 18:37
  • Duplicate of https://stackoverflow.com/questions/5947620/is-there-a-nicer-way-to-do-c-named-arguments ? https://stackoverflow.com/questions/48264698/a-way-to-emulate-named-arguments-in-c – KamilCuk Sep 04 '21 at 18:41
  • 1
    It helps to know what features are called, and communicate clearly. I didn't understand that you were mostly interested in the caller's side of the code. That means you're dealing with the *arguments*. So the first step you should think of taking in order to solve your problem, is to put `c++ named arguments` into a search engine. Keep in mind that [you are expected to do some research yourself first here](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). – Karl Knechtel Sep 04 '21 at 18:41
  • Read about [C++ date and time utilities](https://en.cppreference.com/w/cpp/chrono) – Basile Starynkevitch Sep 04 '21 at 18:45
  • 1
    I voted to reopen because, IMO, the question is clear (re. C/C++ equiv of named parameters). It _might_ be a duplicate of the link(s) KamilCuk cited, but I like dbush's answer here. The most thoughtful/thorough explanation is the github link, but we can't mark as a duplicate a non-SE link – Craig Estey Sep 04 '21 at 19:35
  • 1
    Beware the [XY problem](https://en.wikipedia.org/wiki/XY_problem), something you *nearly* fell prey to. Your **real** question is **not** *"How to use named parameters in C++?"* but rather *"How to handle a function with many parameters in C++?"* Ask about the root problem. You can (and did) use Python's approach to illustrate how one language approached this, but do not assume that what works in one language has a direct analog in another. Different languages may use different approaches to the same hurdle. – JaMiT Sep 05 '21 at 03:58

3 Answers3

8

Neither C nor C++ supports named parameters.

The closest you can come to this is to define a struct with all of the parameters:

struct cal_params {
    int year;
    int month;
    int day;
};

Define the function to take an instance of that struct:

char *calendar(struct cal_params params)
{
    ...
}

Then call the function with a compound literal using designated initializers:

char *str = calendar((struct cal_params){ .year=2021, .month=9, .day=4});
dbush
  • 205,898
  • 23
  • 218
  • 273
3

In C++20, a designated initializer was added, but the initialization must be in order according to the order of declaration.

struct date_t {
  int year, month, day;
};

std::string make_calendar(date_t date) {
  return std::format("{0}:{1}:{2}", date.year, date.month, date.day);
}

Then, you can call it like:

make_calendar({.year = 2001, .month = 9, .day = 4})
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
2

C++ does not have this feature. As shown in this answer you can emulate it by naming members of a struct. Alternatively you can name the individual types of the arguments:

#include <string>
#include <iostream>

struct Year { int value; };
struct Month { int value; };
struct Day { int value; };

std::string calendar(Year y,Month m,Day d)
{
    return std::to_string(y.value) + "-" + std::to_string(m.value) + "-" + std::to_string(d.value);
}

int main ()
{       
    std::cout<<calendar(Year{2001},Month{31},Day{42});
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185