FUNCTION DEFAULTS, RECURSION, AND OVERLOADING Default Parameter Values The inline Directive Recursion Function Overloading Using One Function as a Pattern for Many Default Parameter Values Function prototype can declare default values for function parameters float sum (float = 1.0, float = 0.0); float sum (float num1, float num2) { return (num1 + num2); } z = sum (5.2, 3.7); // uses no defaults z = sum (5.2); // uses 0.0 default z = sum (); // uses both defaults Function call -- if value omitted, all parameters to its right must be omitted as well All omitted parameters need to have default values int add (int = 1, int = 0, int = 4); a = add (5,3); a = add (5); a = add (); Each parameter need not have default value int add (int, int = 0, int = 4); The inline Directive Function calls reduce memory used by program Function calls more time-consuming at run-time than equivalent imbedded code Inline function -- compiler inserts function body at point of call In some cases can greatly improve the run-time performance of program inline return-type function (parameters) Compiler may ignore any such directive inline float sum (float num1, float num2) { return (num1 + num2); } Only small functions should be considered for inlining -- space wasted when repeating large chunks of code Recursion In recursion a function calls itself either directly or indirectly Factorial -- iterative version facto(number); int facto (int n) { int product; int ctr; product = 1; for (ctr=1; ctr<=n; ctr++) product = product * ctr; return (product); } Factorial -- recursive version facto(number); int facto (int n) { int product; if (n==1) product = 1; else product = n * facto(n-1); return (product); } Must always be a way for the recursive function to end Recursive solutions often elegant and simple Recursion slower and takes more memory Beware of Accidental Recursion! sum(); int sum { int total; int i; total = 0; for (i=1; i<=10; i++) total = sum + i; return (total); } Function Overloading Sometimes two (or more) functions very similar in what they do Function Overloading -- using same function name for two or more distinct functions // function prototypes for two quick_sort functions void quick_sort (int [], int); void quick_sort (float [], int); void main () { int r[100]; float s[25]; ... quick_sort (r,100); quick_sort (s,25); } Overloading functions only possible when parameter lists differ in at least one parameter Functions must have different number of parameters or at least one parameter must be different type Functions that differ only in type of return value may not have same name Be careful about default and overloading ambiguity... float sum (float, float); float sum (float, float, float=5.3); ... x = sum (a,b); Using One Function as a Pattern for Many int sum (int num1, int num2) { return (num1 + num2); } float sum (float num1, float num2) { return (num1 + num2); } Two sum functions Same except for different parameter types and different return types Nearly identical functions can occur when several overloaded functions do same things with different type parameters Better solution is to use Function Template Function Templates used as general patterns to create actual functions (called Template Functions) Function template type parameters are terms programmer invents as stand-ins Replaced at compile time by actual data types Used to represent type of function return value and types of function parameters Can be used anywhere type can be used in function body (for example, local declarations)