Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.

Similar presentations


Presentation on theme: "Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without."— Presentation transcript:

1 Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without an argument for the “defaulted” parameter The parameter is then assigned the default value

2 Example: Function with defaults
int sum(int i=2, int j=3); main() { int x=4, y=5, z; z= sum(x,y); z= sum(x); z= sum(); } int sum(int i, int j) { return i+j; Function is defined with default values for for both inputs i and j Defaults appear where the function is defined The function can then be called with or without arguments for the parameters with defaults defined If an argument is missing, the default value gets passed to the function

3 Defaulting “Rules” Any parameter may be given a default
“Defaulted” parameters can be of any type Any number of parameters may be given a default value i.e. defaults can be limited to a subset of parameters Default parameters are defined when the function is declared Usually in the prototype (unless there isn’t one)

4 Default parameter order
A subset of parameters may be assigned defaults, but… All “defaulted” parameters must appear last in the parameter list “Non-defaulted” parameters must be first and there can be no gaps! Examples: func(int a, int b, int c=5); // valid func(int a, int b=10, int c); // invalid !!!! “Non-defaulted” parameter (c) appears after parameter with a default value (b)

5 Invocation Valid calls: Invalid:
When a function containing default parameters is called, defaults are assigned in reverse order – last to first. Parameters with no default assignment must be called with an argument All default assignments are based on parameter order, not type! Example: func(int a, string b=“foo”, int c=5, int d=10); What values do a, b, c, and d get in the valid calls shown to the right? Valid calls: int x=3; func(x); func(x,”bar”); func(x,”bar”,6); func(x,”bar”,6,7); Invalid: func(); func(x,6,7); func(6,6,”bar”);

6 Some wrinkles… The definitions to the right are valid
#include <iostream> using namespace std; int sum(int i=2, int =3); int sum1(int i=6, int j=7) { return i+j; } main() { int x=4, y=5, z; z= sum(x,y); cout << z << endl; z= sum(x, 6); z= sum(); z= sum1(); int sum(int ii, int jj) { return ii+jj; The definitions to the right are valid Note the following: Variable name not given Don’t need the variable name in the prototype (not really “used”) Variable names don’t match No prototype, function declared and defined all at once


Download ppt "Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without."

Similar presentations


Ads by Google