.. _functions-parameters-and-arguments: Parameters and Arguments ------------------------ .. index:: single: parameter Some of the built-in functions we have used have **parameters**, which are values that you provide to let the function do its job. For example, if you want to find the sine of a number, you have to indicate what the number is. Thus, ``sin`` takes a ``double`` value as a parameter. Some functions take more than one parameter, like ``pow``, which takes two ``double`` s, the base and the exponent. Notice that in each of these cases we have to specify not only how many parameters there are, but also what type they are. So it shouldn't surprise you that when you write a function definition, the parameter list indicates the type of each parameter. For example: :: void print_twice (char phil) { cout << phil << phil << '\n'; } This function takes a single parameter, named 'phil', that has type ``char``. Whatever that value is (and at this point we have no idea what it is), it gets printed twice, followed by a new line. I chose the name phil to suggest that the name you give a parameter is up to you, but in general you want to choose something more illustrative than phil. In order to call this function, we have to provide a ``char``. For example, we might have a ``main`` function like this: :: int main () { print_twice ('a'); return 0; } .. index:: single: argument .. index:: single: pass The ``char`` value you provide is called an **argument**, and we say that the argument is **passed** to the function. In this case the value ’a’ is passed as an argument to print_twice where it will get printed twice. Alternatively, if we had a ``char`` variable, we could use it as an argument instead: :: int main () { char argument = 'b'; print_twice (argument); return 0; } Notice something very important here: the name of the variable we pass as an argument (argument) has nothing to do with the name of the parameter (phil). Let me say that again: .. note:: The name of the variable we pass as an argument has nothing to do with the name of the parameter. They can be the same or they can be different, but it is important to realize that they are not the same thing, except that they happen to have the same value (in this case, the character ’b’). .. caution:: The value you provide as an argument must have the same **type** as the parameter of the function you call. This rule is important, but it is sometimes confusing because C++ sometimes converts arguments from one type to another automatically. Recall the section :ref:`functions-converting-from-double-to-int`. What is true in expressions is also true for function arguments. For now focus on the general rule, and we will deal with exceptions later. .. tb-group:: :name: tab_check .. tb-tab:: Q1 The following code will show the output of the print_twice function. Try modifying the value of argument to change the output. .. tb-code:: cpp :name: params_args_AC_1 :caption: Parameter Practice #include void print_twice (char phil) { std::cout << phil << phil << '\n'; } int main () { char argument = 'b'; print_twice (argument); return 0; } .. tb-tab:: Q2 .. tb-blank:: :name: params_args_1 :: #include void print_twice (char phil) { std::cout << phil << phil << '\n'; } int main () { string argument = "s"; print_twice (argument); return 0; } What will be printed to the terminal? If nothing will print, or if the compiler will throw an error, type "error". {{blank}} .. tb-answer:: :match: error :feedback: The type provided does not match the expected parameter type! :hint: ss; The type provided does not match the expected parameter type! :incorrect: Try again! .. tb-tab:: Q3 .. tb-blank:: :name: params_args_2 :: #include void print_twice (char phil) { std::cout << phil << phil << '\n'; } int main () { char argument = 'b'; print_twice (argument); return 0; } What will be printed to the terminal? If nothing will print, or if the compiler will throw an error, type "error". {{blank}} .. tb-answer:: :match: bb :feedback: The print_twice fucntion will print the character argument proveided two times. :hint: error; Something will be printed to the terminal! :incorrect: Try again! .. tb-tab:: Q4 .. tb-match:: :name: params_args_4 Match the function declaration to an example of its function call. int times_two(int x, int y); times_two(4, 7); int times_two(string y, int x); times_two("hello", 10); int times_two(double x, string y); times_two(4.5, "hello"); int times_two(string x, string y); times_two("hello", "hi"); ----- .. admonition:: More to Explore - From cppreference.com - :lang:`Function definitions ` and :lang:`declarations` - :lang:`Functions `