ArticleslgStudy

science

Default argument

Default argument is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Default argument rather than just read about it. In short: In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments.

Key takeaways

  • Default argument belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Default argument to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Default argument from memory before moving on to harder problems.

Reference excerpt

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments. Usually, each argument must be specified in full (this is the case in the C programming language). Later languages (for example, in C++) allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function.

Default arguments in C++ Consider the following function declaration:

This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways:

In the first case the value for the argument called c is specified explicitly. In the second case, the argument is omitted, and the default value of 12 will be used instead. For the function called, there is no means to know if the argument has been specified by the caller or if the default value was used. The above-mentioned method is especially useful when one wants to set default criteria so that the function can be called with or without parameters. Consider the following:

The function call printGreeting(); will by default print "Hello, world!" to the standard output stream. On the other hand, any object of type std::ostream can now be passed to the same function and the function will print to the given stream instead of to the standard output. Meanwhile, printGreeting(std::cerr); sets the stream parameter to std::cerr, and thus prints the output the standard error stream. Because default arguments' values are "filled in" at the call site rather than in the body of the function being called, virtual functions take their default argument values from the static type of the pointer or reference through which the call is made, rather than from the dynamic type of the object supplying the virtual function's body.

Overloaded methods Some languages, such as Java, do not have default arguments. However, the same behaviour can be simulated by using method overloading to create overloaded methods of the same name, which take different numbers of arguments; and the versions with fewer arguments simply call the versions with more arguments, with the default arguments as the missing arguments:

However, in addition to several other disadvantages, since the default arguments are not modeled in the type system, the type of a callback (aka higher-order function) can't express that it accepts either of the overloads nor simulate the default arguments with overloaded functions. Whereas, in JavaScript the non-overloaded function definition can substitute the default when the input value is undefined (regardless if it was implicitly undefined via the argument's absence at the call site or an explicitly passed undefined value); which is modeled as an optional argument parameter type ?: in TypeScript. JavaScript's solution is not resolved statically (i.e. not at compile-time, which is why TypeScript models only the optionality and not the default values in the function's type signature) thus incurs additional runtime overhead, although it does provide more flexibility in that callbacks can independently control their defaults instead of centrally dictated by the (callback's type signature in the) type signature of the function which inputs the callback. The TypeScript solution, for example, can be simulated in Java with the java.util.Optional<T> type except the analogous of an implicit undefined for each absent argument is an explicit Optional.<Integer>absent() at the call site; an absent value is given with Optional.empty().

Default type parameters In some languages, such as C++, the type parameter of a generic may have a default type.

Evaluation For every function call default argument values must be passed to the called function. If a default argument value contains side-effects, it is significant when those side effects are evaluated – once for the entire program (at parse time, compile time, or load time), or once per function call, at call time. Python is a notable language that evaluates expressions in default arguments once, at the time the function declaration is evaluated. If evaluation per function call is desired, it can be replicated by having the default argument be a sentinel value, such as None, and then having the body of the function evaluate the default value's side effects only if the sentinel value was passed in. For example:

Extent Generally a default argument will behave identically to an argument passed by parameter or a local variable declared at the start of the function, and have the same scope and extent (lifetime) as a parameter or other local variable, namely an automatic variable which is deallocated on function termination. In other cases a default argument may instead be statically allocated. If the variable is mutable, it will then retain its value across function calls, as with a static variable. This behavior is found in Python for mutable types, such as lists. As with evaluation, in order to ensure the same extent as a local variable, one can use a sentinel value:

References

Worked examples

Example 1 — a first encounter with Default argument

Start with the simplest possible case. Write down what Default argument claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Default argument before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Default argument ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Default argument

In research
Default argument appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Default argument in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Default argument is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, Subroutines, so understanding it makes those chapters shorter.
In everyday life
Look for Default argument outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Default argument” →

How to study Default argument in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Default argument means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Default argument out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Default argument in simple terms?

In computer programming, a default argument is an argument to a function that a programmer is not required to specify. In most programming languages, functions may take one or more arguments.

Why does Default argument matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Default argument?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Default argument.

Tags

  • C++
  • Subroutines

Keep exploring