♦ C++ Function
♣ About C++ Function
Function is block of code which runs when a Function is invoked.Function can accept arguments for execution.
The very usefulness of Function lies in the saying "define once use over and over", that is, a function can be defined once and can be invoked over and over again.
Function code modification is another time-saving aspect, that is, it is sufficient to modify the Function code once and the rest of the function's invocations will automatically be modified.
♣ Function Declaration in C++
Below is the representation of C++ Function Syntax.Basic Syntax
returnDataType myFunction(parameter1, parameter2, ...) {
// code for execution
return returnValue;
}
// code for execution
return returnValue;
}
Syntax Explanation:
returnDataType: data type of the return value
myFunction: name of the function
parameter1, parameter2, ... : input parameters of the function
returnValue: the value which function returns upon completion of execution
myFunction: name of the function
parameter1, parameter2, ... : input parameters of the function
returnValue: the value which function returns upon completion of execution
♣ Example1: Adding Two Numbers with Function in C++
In this example, A Function is adding two Integers. Example
Input:
Output:
♣ Recursion of Function in C++
A Function can be self-called over and over again to get a task-completed.For a Function recursion to happen, it is essential to have at least one Function call inside the Function block itself.
♣ Example2:Calculating Factorial of a number with Recursion in C++
In this example, Function calculates Factorial of a Number with Function Recursion. Example
Input:
Output: