A function is a collection of statements that accept inputs, do a given computation, and return output. The aim is to combine some regularly or repeatedly performed actions into a function so that we may call it instead of writing the same code for different inputs.
C++ has two Types of Functions:
Library functions are pre-written functions included in the C++ Standard Library. They handle common activities and procedures like input/output, mathematical calculations, and string manipulation.
User-defined functions are those created by the programmer to fulfill specific tasks. They help to organize code, reduce redundancy, and improve readability. A user-defined function is declared with a return type, name, and parameters, and it is defined with a block of code that performs the desired tasks.
A function declaration, a function prototype, describes the function's name, return type, and parameters but does not include the function body. It informs the compiler what to expect from the function.
A function call runs the function's code. It entails naming the function and supplying the required arguments to it. Functions are known by their names. If the function has no arguments, it can be called straight by its name. However, we can refer to functions with arguments in three different ways:
When calling by value, the original value is not changed. The function parameter stores the passed-in value in the stack memory address. Changing the value of a function parameter affects only the current function. This will not alter the value of variables within the caller method, such as main().
We modify the original value by passing a reference (address). The address of the value is passed in the function, thus both the actual and formal parameters use the same address space. Value changes within the function are reflected both internally and externally.
The method of sending arguments to a function involves copying the address of an argument into the formal parameter. The function uses the address to get the call's real parameter. This signifies that changes to the parameter affect the supplied argument. Declare function parameters as pointer types. For example, swap() exchanges the values of two integer variables pointed to by its arguments.
A function definition consists of the function declaration and the function body, which contains the code executed when the function is called.
Functions can accept information as a parameter. They serve as variables within the function. They are specified within the parentheses following the function name. A comma can be used to separate several parameter values.
A function prototype is a function declaration that has only the function's name, return type, and parameters. It informs the compiler about the function.
The return statement terminates the execution of a function and may optionally return a value to the function caller. If the function has a void return type, no value is returned.
Function overloading is a programming feature in which two or more functions have the same name but accept different numbers or types of inputs. This enables the same function name to be used for several purposes depending on the input parameters.
A default argument is a value specified in a function declaration that the compiler will assign if the caller function does not give a value for the argument. Passing a value overrides the default value.