Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Positional and Default Arguments | Function Arguments Specification
C++ Functions

bookPositional and Default Arguments

Positional arguments

Positional arguments are a way of passing values to a function in a specific order based on the function's signature. The values are associated with parameters according to their positions in the parameter list of function signature. The position of each argument determines which parameter it corresponds to.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include <iostream> // Function that takes two positional arguments float divide(float a, float b) { if (b == 0) return 0; return a / b; } int main() { float x = 8; float y = 4; // Calling the 'divide' function with two positional arguments float result = divide(x, y); std::cout << result << std::endl; }

You can conduct a small experiment: change the order of passing arguments to the function (use divide (y, x) instead of divide(x, y)), and you will see that the result has changed. This is precisely the essence of positional arguments: the order of passing parameters to the function is important, directly affecting its result.

Default arguments

Default arguments let you assign default values to function parameters. If a function is called without providing all arguments, the missing ones use their default values. To set a default, just give the parameter a value directly in the function declaration.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> // Default value is set for the third parameter float divide(float a, float b, bool terminate = true) { if (b == 0) { if (terminate) return 0; return a / 0.000001; // Prevent crash by using a very small number } return a / b; } int main() { std::cout << divide(10, 2) << std::endl; // Normal division std::cout << divide(10, 0) << std::endl; // Default behavior (error) std::cout << divide(10, 0, false) << std::endl; // Forced division }

The divide() function divides one number by another. If the denominator (b) is zero:

  • terminate = true β†’ returns 0
  • terminate = false β†’ divides by a tiny number to avoid an error

The terminate parameter has a default value of true, so it can be omitted. Set terminate = false to continue the calculation instead of stopping.

Note
Note

It is important that all default arguments are located after the positional ones in the list of parameters. Otherwise, the compiler will throw an error.

question mark

Which function declaration correctly uses positional and default arguments?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookPositional and Default Arguments

Swipe to show menu

Positional arguments

Positional arguments are a way of passing values to a function in a specific order based on the function's signature. The values are associated with parameters according to their positions in the parameter list of function signature. The position of each argument determines which parameter it corresponds to.

main.cpp

main.cpp

copy
1234567891011121314151617181920
#include <iostream> // Function that takes two positional arguments float divide(float a, float b) { if (b == 0) return 0; return a / b; } int main() { float x = 8; float y = 4; // Calling the 'divide' function with two positional arguments float result = divide(x, y); std::cout << result << std::endl; }

You can conduct a small experiment: change the order of passing arguments to the function (use divide (y, x) instead of divide(x, y)), and you will see that the result has changed. This is precisely the essence of positional arguments: the order of passing parameters to the function is important, directly affecting its result.

Default arguments

Default arguments let you assign default values to function parameters. If a function is called without providing all arguments, the missing ones use their default values. To set a default, just give the parameter a value directly in the function declaration.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> // Default value is set for the third parameter float divide(float a, float b, bool terminate = true) { if (b == 0) { if (terminate) return 0; return a / 0.000001; // Prevent crash by using a very small number } return a / b; } int main() { std::cout << divide(10, 2) << std::endl; // Normal division std::cout << divide(10, 0) << std::endl; // Default behavior (error) std::cout << divide(10, 0, false) << std::endl; // Forced division }

The divide() function divides one number by another. If the denominator (b) is zero:

  • terminate = true β†’ returns 0
  • terminate = false β†’ divides by a tiny number to avoid an error

The terminate parameter has a default value of true, so it can be omitted. Set terminate = false to continue the calculation instead of stopping.

Note
Note

It is important that all default arguments are located after the positional ones in the list of parameters. Otherwise, the compiler will throw an error.

question mark

Which function declaration correctly uses positional and default arguments?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt