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.
This commit is contained in:
		| @@ -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. | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user