Why define your own functions?
Readability
Maintainability
Code reuse
Cpp Code
# Include <iostream>
Using namespace std;
Int raiseToPower (int base, int exponent ){
Int result = 1;
For (int I = 0; I <exponent; I = I + 1 ){
Result = result * base;
}
Return result;
}
Int main (){
Int threeExpFour = raiseToPower (3, 4 );
Cout <"3 ^ 4 is" <threeExpFour <endl;
Return 0;
}
Function delaration shocould be set before function invocation.
Returning a value
Up to one value may be returned
Must be the same type as the return type
No need to be at the end
Returns the value as soon as a return statement is executed
Fucntion overloading
Cpp Code
Void printOnNewLine (int x)
{
Cout <"1 Integer:" <x <endl;
}
Void printOnNewLine (int x, int y)
{
Cout <"2 Integers:" <x <"and" <y <endl;
}
Function prototypes are generally put into separate header files.
Recursion
Functions can call themselves
Base case
Recursive step
Pass by value (a) vs pass by reference (&)