Quantcast
Channel: Comments on: 7.7 — Default parameters
Browsing all 48 articles
Browse latest View live

By: Akshay Chavan

In foo.h 1 void printvalues(int x, int y=10); Shouldn’t it be 1 void printValues(int x, int y=10);

View Article


By: Alex

Yes, fixed.

View Article


By: blaze077

"Note that it is impossible to supply a user-defined value for z without also supplying a value for x and y." This function refers to printValues(int x, int y, int z) so I think the sentence should be:...

View Article

By: Alex

Lesson fixed. Thanks!

View Article

By: Kess

Actually, I believe it was correct in it’s former version, as in order to provide the value only for x you could just write printValues(3); and the function would be called with 3, 20 and 30 as its...

View Article


By: Alex

Agreed -- I’ve updated and clarified the text to be clearer that I was talking about arguments, not parameters. 🙂

View Article

By: nikos-13

"void printStringInColor(std::string, Color color=COLOR_BLACK); // Color is an enum" You forgot the parameter’s, of type std::string, name.

View Article

By: Alex

Fixed. Even though providing the name of the parameters is optional in a function prototype (only the type is required), it’s a good practice to do so.

View Article


By: nikos-13

Oh, I didn’t know that…

View Article


By: Nakamura

How does it work for map? I have a map "std::map<int,std::string> map" , how will it work for optional parameter ?

View Article

By: nascardriver

Hi Nakamura! An std::map can be initialized with empty curly braces 12345678910111213141516 #include <iostream>#include <map>#include <string> void fn(std::map<int,std::string>...

View Article

By: Marcus

Is there an explicit way to use the default value? e.g printValues(-1, *, -1) where '*' means that I would like to use the default value. Output: Values: -1 20 -1

View Article

By: nascardriver

Hi Marcus! The only way to use default arguments is not setting them at all.

View Article


By: Marcus

;( Thank you nascardriver!

View Article

By: Alex

Short answer: no. Default values can only be invoked for the rightmost parameters in a call, and are invoked by not passing an argument for that parameter in the function call.

View Article


By: Trevor

I should probably try it, but I would think that if a function has default arguments and is called from a different CPP file, then the default values would need to be in the function prototype. My...

View Article

By: jayu

char name[10]; //I want to enter default value of name like "name= jayu"how I can do that please send me syntax of this.jayupipaliya198@gmail.com

View Article


By: nascardriver

Hi Jayu! 123 const char *szName{ "jayu" }; // Immutable (Can't be modified)// Orchar arrName[4]{ "jayu" }; // Mutable (Can be modified)

View Article

By: Alex

I believe you are correct. This is generally avoided by putting your function prototypes in a header and #including that header wherever you need access to the function. That way you only have a single...

View Article

By: Peter Baum

1. Is there any good reason why C++ does not support more flexible default values such as 1 printValues(,,3) and 1 void printValue(int x=10, int y); ? 2. It seems strange that “Once declared, a default...

View Article
Browsing all 48 articles
Browse latest View live