From a3b20c065232050f96549f1d93e2062775313e8a Mon Sep 17 00:00:00 2001 From: Yves Wienecke <30377629+iyves@users.noreply.github.com> Date: Sun, 28 Oct 2018 18:27:41 -0700 Subject: [PATCH] Return types, pass by reference vs pass by value (#20261) Expanded on what data types can be returned. Also added section for the differences between passing an object as a parameter by value versus by reference. --- guide/english/cplusplus/functions/index.md | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/guide/english/cplusplus/functions/index.md b/guide/english/cplusplus/functions/index.md index 4a0fbe180c..9b4ac0ec35 100644 --- a/guide/english/cplusplus/functions/index.md +++ b/guide/english/cplusplus/functions/index.md @@ -19,7 +19,15 @@ return_type function_name( parameter list ) ``` ### Return type: -A function may return a value. The `return_type` is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the `return_type` is the keyword `void`, but the [return type of main() must always be int](https://stackoverflow.com/a/4207223/). +A function may return a value. The `return_type` is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the `return_type` is the keyword `void`, but the [return type of main() must always be int](https://stackoverflow.com/a/4207223/). In C++, the return type of a function can be any primitive data type, a reference to an object, or a pointer to an object. +```cpp +int func1(); // Returns a primitive data type +char & func2(); // Returns a reference to an object (Careful not to return a obeject local to the function) +float * func3(); // Returns a pointers to a float + +struct node{}; +node * func4(); // Returns a pointer to a user defined data type +``` ### Function name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. @@ -27,6 +35,36 @@ This is the actual name of the function. The function name and the parameter lis ### Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. +The parameters of a function are passed by value unless explicitly specified by the programmer. This means that the program will create a temporary copy of the arguments, and any changes made to those copies from within the function will not remain once the function returns. + +The address of operator, `&`, allows the programmer to pass in a parameter by reference. When the function is invoked, the program will not create a copy of the parameter, and any changes made from within the function will remain once the function returns. + +```cpp +// Function prototypes +void increment_by_value (int a); +void increment_by_reference (int & a); + +int main() { + int number = 0; + + // Invoking functions + increment_by_value(number); // The value of number stays at 0 + increment_by_reference(number); // The value of number changes to 1 + return 1; +} + +// Function definitions +void increment_by_value (int a) { + a = a + 1; + return; +} + +void increment_by_reference (int & a) { + a = a + 1; + return; +} +``` + ### Function body: The function body contains a collection of statements that define what the function does.