Questions tagged [default-arguments]

A default argument is a value passed to a certain parameter of a function if the user does not explicitly supply a value for that parameter.

A default argument is a value passed to a certain parameter of a function if the user does not explicitly supply a value for that parameter.

Some language extend this concept to other entities that have parameters. An example is C++, which has default template arguments, which is a value, type or template that is provided as an argument to a template parameter if the user does not explicitly provide an argument.

356 questions
-1
votes
1 answer

Function argument default value on a ternary condition

Trying to use the default value for argument of a function, but it always return as if the variable was null. function a($param = "defaultValue") { echo( $param ? $param : 'Hello World'); //echo( isset($param) ? $param : 'Hello…
PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
-1
votes
1 answer

c++ default function parameter dont work

I have a function which needs default parameter: LINE 13: void genRand(double offset_x = 0.0, double offset_y = 0.0); This is the function: LINE 84: void game::genRand(double offset_x = 0.0, double offset_y = 0.0) { …
-2
votes
1 answer

Default arguments on virtual methods

I'm using Clang-Tidy to inspect my code. One of the tool's messages puzzled me: clang-tidy: Default arguments on virtual or override methods are prohibited I get the idea when I actually use a method override. But here in my case, I want to…
-2
votes
1 answer

'name not defined' when setting a class to a variable

Sorry for the dumb question, I can't seem to find the answer anywhere. I'm trying to convert my class into a variable but it gives me an error NameError: name 'period' is not defined class Stuff(): def __init__(self, period = 1, division =…
coinmaster
  • 133
  • 6
-2
votes
2 answers

Default arguments C++ for all parameters that exists

I am looking for an example of code that shows that there are some parameters types which can't be assigned with default arguments. I mean, I want to disprove the statement "You can define a default argument for every parameter in a function", or,…
user13489726
-2
votes
3 answers

C++ set default arguments with #define

Can I set default arguments to a #defined value? #define WIDTH 1920 #define HEIGHT 1080 void calculate(int width = WIDTH, int height = HEIGHT) { //do stuff }
icanfathom
  • 291
  • 4
  • 11
-2
votes
1 answer

Default argument value is overriding provided argument?

I have a function in PHP, which has some arguments default to null, so that I can easily call it with less than the full number of arguments. The problem is, that when I use a null-defaulted argument directly, I get the given argument, but when I…
E.T.
  • 598
  • 3
  • 21
-3
votes
2 answers

The using declaration and function default arguments

According to the C++ 17 Standard (10.3.3 The using declaration) 1 Each using-declarator in a using-declaration98 introduces a set of declarations into the declarative region in which the using-declaration appears. and 10 A using-declaration is a…
-3
votes
2 answers

Using default value for const reference argument causes crash

I have some code like this: struct Data { Data(const std::vector &data = {}) : data_(data) {} const std::vector &data_; }; Data create1() { return Data(); // bad } Data create2() { return Data({}); // bad } Data…
Fan
  • 315
  • 3
  • 11
-3
votes
1 answer

Is there a better alternative to keeping a mutable optional argument as a parameter without its value persisting on subsequent calls in Python?

Inappropriately marked as a duplicate question. This has nothing to do with not knowing how to pass a variable by reference. It is asking if there is a way to mitigate the issues with keeping a mutable optional argument as a parameter without its…
Dan Chrostowski
  • 337
  • 2
  • 8
-5
votes
2 answers

I am unable to use a class variable as a default argument for the same class's function

I am well aware that line 7 is invalid . but I want to use the class variables as default argument to method(apple) . class trial{ public: int i=10 ; void apple(int i=this.i){ cout<
1 2 3
23
24