Commit abb2232f authored by GILLES Sebastien's avatar GILLES Sebastien
Browse files

Functions: add optional arguments.

parent 4c12e8b0
......@@ -14,7 +14,7 @@
},
"source": [
"<h1>Table of contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Function-definition\" data-toc-modified-id=\"Function-definition-1\">Function definition</a></span><ul class=\"toc-item\"><li><span><a href=\"#Passing-arguments-by-value\" data-toc-modified-id=\"Passing-arguments-by-value-1.1\">Passing arguments by value</a></span></li><li><span><a href=\"#Passing-arguments-by-reference\" data-toc-modified-id=\"Passing-arguments-by-reference-1.2\">Passing arguments by reference</a></span></li><li><span><a href=\"#A-bit-of-wandering:-using-C-like-error-codes\" data-toc-modified-id=\"A-bit-of-wandering:-using-C-like-error-codes-1.3\">A bit of wandering: using C-like error codes</a></span></li><li><span><a href=\"#Passing-arguments-by-pointers\" data-toc-modified-id=\"Passing-arguments-by-pointers-1.4\">Passing arguments by pointers</a></span></li></ul></li><li><span><a href=\"#Function-with-return-value\" data-toc-modified-id=\"Function-with-return-value-2\">Function with return value</a></span></li><li><span><a href=\"#Function-overload\" data-toc-modified-id=\"Function-overload-3\">Function overload</a></span><ul class=\"toc-item\"><li><span><a href=\"#The-easy-cases:-arguments-without-ambiguity\" data-toc-modified-id=\"The-easy-cases:-arguments-without-ambiguity-3.1\">The easy cases: arguments without ambiguity</a></span></li><li><span><a href=\"#[WARNING]-Return-type-doesn't-count!\" data-toc-modified-id=\"[WARNING]-Return-type-doesn't-count!-3.2\">[WARNING] Return type doesn't count!</a></span></li><li><span><a href=\"#Good-practice:-don't-make-signature-vary-only-by-a-reference-or-a-pointer\" data-toc-modified-id=\"Good-practice:-don't-make-signature-vary-only-by-a-reference-or-a-pointer-3.3\">Good practice: don't make signature vary only by a reference or a pointer</a></span></li><li><span><a href=\"#Best-viable-function\" data-toc-modified-id=\"Best-viable-function-3.4\">Best viable function</a></span></li><li><span><a href=\"#Advice:-use-overload-only-when-there-is-no-ambiguity-whatsoever\" data-toc-modified-id=\"Advice:-use-overload-only-when-there-is-no-ambiguity-whatsoever-3.5\">Advice: use overload only when there is no ambiguity whatsoever</a></span></li></ul></li><li><span><a href=\"#Lambda-functions\" data-toc-modified-id=\"Lambda-functions-4\">Lambda functions</a></span></li><li><span><a href=\"#Passing-a-function-as-a-an-argument\" data-toc-modified-id=\"Passing-a-function-as-a-an-argument-5\">Passing a function as a an argument</a></span></li><li><span><a href=\"#A-very-special-function:-main\" data-toc-modified-id=\"A-very-special-function:-main-6\">A very special function: <strong>main</strong></a></span></li><li><span><a href=\"#inline-functions\" data-toc-modified-id=\"inline-functions-7\"><code>inline</code> functions</a></span></li></ul></div>"
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Function-definition\" data-toc-modified-id=\"Function-definition-1\">Function definition</a></span><ul class=\"toc-item\"><li><span><a href=\"#Passing-arguments-by-value\" data-toc-modified-id=\"Passing-arguments-by-value-1.1\">Passing arguments by value</a></span></li><li><span><a href=\"#Passing-arguments-by-reference\" data-toc-modified-id=\"Passing-arguments-by-reference-1.2\">Passing arguments by reference</a></span></li><li><span><a href=\"#A-bit-of-wandering:-using-C-like-error-codes\" data-toc-modified-id=\"A-bit-of-wandering:-using-C-like-error-codes-1.3\">A bit of wandering: using C-like error codes</a></span></li><li><span><a href=\"#Passing-arguments-by-pointers\" data-toc-modified-id=\"Passing-arguments-by-pointers-1.4\">Passing arguments by pointers</a></span></li></ul></li><li><span><a href=\"#Function-with-return-value\" data-toc-modified-id=\"Function-with-return-value-2\">Function with return value</a></span></li><li><span><a href=\"#Function-overload\" data-toc-modified-id=\"Function-overload-3\">Function overload</a></span><ul class=\"toc-item\"><li><span><a href=\"#The-easy-cases:-arguments-without-ambiguity\" data-toc-modified-id=\"The-easy-cases:-arguments-without-ambiguity-3.1\">The easy cases: arguments without ambiguity</a></span></li><li><span><a href=\"#[WARNING]-Return-type-doesn't-count!\" data-toc-modified-id=\"[WARNING]-Return-type-doesn't-count!-3.2\">[WARNING] Return type doesn't count!</a></span></li><li><span><a href=\"#Good-practice:-don't-make-signature-vary-only-by-a-reference-or-a-pointer\" data-toc-modified-id=\"Good-practice:-don't-make-signature-vary-only-by-a-reference-or-a-pointer-3.3\">Good practice: don't make signature vary only by a reference or a pointer</a></span></li><li><span><a href=\"#Best-viable-function\" data-toc-modified-id=\"Best-viable-function-3.4\">Best viable function</a></span></li><li><span><a href=\"#Advice:-use-overload-only-when-there-is-no-ambiguity-whatsoever\" data-toc-modified-id=\"Advice:-use-overload-only-when-there-is-no-ambiguity-whatsoever-3.5\">Advice: use overload only when there is no ambiguity whatsoever</a></span></li></ul></li><li><span><a href=\"#Optional-parameters\" data-toc-modified-id=\"Optional-parameters-4\">Optional parameters</a></span></li><li><span><a href=\"#Lambda-functions\" data-toc-modified-id=\"Lambda-functions-5\">Lambda functions</a></span></li><li><span><a href=\"#Passing-a-function-as-a-an-argument\" data-toc-modified-id=\"Passing-a-function-as-a-an-argument-6\">Passing a function as a an argument</a></span></li><li><span><a href=\"#A-very-special-function:-main\" data-toc-modified-id=\"A-very-special-function:-main-7\">A very special function: <strong>main</strong></a></span></li><li><span><a href=\"#inline-functions\" data-toc-modified-id=\"inline-functions-8\"><code>inline</code> functions</a></span></li></ul></div>"
]
},
{
......@@ -805,19 +805,101 @@
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Optional parameters\n",
"\n",
"It is possible to provide optional parameters in the **declaration** of a function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"// Declaration.\n",
"void FunctionWithOptional(double x, double y = 0., double z = 0.);\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"#include <iostream>\n",
"\n",
"// Definition\n",
"void FunctionWithOptional(double x, double y, double z) // notice the absence of default value!\n",
"{\n",
" std::cout << '(' << x << \", \" << y << \", \" << z << ')' << std::endl;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"// WARNING: Xeus-cling issue!\n",
"\n",
"{\n",
" FunctionWithOptional(3., 5., 6.); // ok\n",
" FunctionWithOptional(3.); // should be ok, but Xeus-cling issue.\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The reason not to repeat them is rather obvious: if both were accepted you may modify one of them and forget to modify the others, which would be a bad design...\n",
"\n",
"There is a way to put it in the same place, that I do not recommend (and your compiler should warn you most of the time): if you do not declare the function beforehand, default arguments may be specified at definition:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
"// Definition\n",
"void FunctionWithOptional2(double x, double y = 0., double z = 0.)\n",
"{\n",
" std::cout << '(' << x << \", \" << y << \", \" << z << ')' << std::endl;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"{\n",
" FunctionWithOptional2(3., 5., 6.); // ok\n",
" FunctionWithOptional2(3.); // should be ok, but Xeus-cling issue.\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In C and C++, arguments are only **positional**: you do not have a way to explicitly set an argument with a name for instance. \n",
"\n",
"Therefore:\n",
"\n",
"* Optional arguments must be put together at the end of the function.\n",
"* You must think carefully if there are several of them and put the less likely to be set manually by the function user at then end. In our example above, if you want do call the function with a `x` and a `z` you must mandatorily also provide explicitly `y`.\n"
]
},
{
"cell_type": "markdown",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment