+ 1

Can I put default argument in a function?

Hi, I was trying to write a program and wanted to assign default arguments inside a function like: int func6(int i=7, string s="Hello"){ cout<<"\""<<s<<"\""; return i; } but i got an error for the first line saying: error: default argument given for parameter 1 of 'int func6(int, std::__cxx11::string)' [-fpermissive]| Are we able to give a default argument for the parameters in the function?

8th Feb 2025, 1:04 PM
La Wun Eain
La Wun Eain - avatar
2 ответов
+ 4
That error message can occur when you are using a function prototype. It seems the default values are being declared in both, the function prototype, AND the function definition. They need to be declared only in one place, and that place has to be the prototype. Solution: Remove the defaults from the actual function definition and keep them in the prototype only. Or remove the function prototype altogether and move the function definition (with default definitions) so that it is defined before main().
8th Feb 2025, 4:22 PM
Brian
Brian - avatar
0
Your code has syntax errors...You can define a base class, with run time polymorphism for the method having different types of parameters and / or not different numbers of parameters. These values can be passed by value or reference *p or &p. In the latter case you can overwrite the initial value as required. Pointer arithmetic and stack level access are not supported by 4Gen languages like Python, which don't allow direct access to the kernel. In C# this was clubbed as "managed code" ! https://photos.app.goo.gl/LgnoHp5Cvc59vPri7
10th Feb 2025, 7:13 AM
Sanjay Kamath
Sanjay Kamath - avatar