CS 107 - 7/13/15 Functions - Subsection of code - can be called multiple times - returns to just-after the calling code when finished - has the ability to take parameters, allowing different data sets to be used on the same code - allows a program to be decomposed into smaller parts. - reduces the overall complexity of the program. - each of these smaller pieces can be developed, tested, verified by themselves. - complexity compared to lines-of-code does does have a linear relationship - a task/programming project that can be broken into smaller sections of independent code can be developed quicker than solving the same task using one monolithic main() function. Syntax of a function ( ) { ; } The is a single value that the function may return (or send back) to the calling code. IF the function is not returning a value, the is "void". void printMyInfo (int projectNumber) { cout << "Pat Troy" << endl; cout << "CS 107" << endl; cout << "Summer 2015" << endl; cout << "Lab Assignment " << projectNumber << endl; return ; // valid for void functions - but not needed } int main () { printMyInfo(4); // function calling statement ... return 1; } This is an example of a void function (that does NOT return a value) that encapsulates a single task and reduces the complexity of main(). The can contain zero or more comma separated variable/parameter declarations. These variable/parameter as initialized using the values specified in the function calling statement int max ( int val1, int val2 ) { int returnValue; if ( val1 > val2 ) returnValue = val1; else returnValue = va;2; return returnValue; } int main () { int num1, num2, num3, num4; num1 = 9; num2 = 23; num3 = 43, num4 = 18; int larger; larger = max ( num1, num2 ); cout << max (num3, num4) << endl; int weirdSum = max (num1, num3) + max (num2, num4) ; Overloading of Function Names occurs when we have two functions with the same name. This is allowed as long as the two functions have different "parameter signatures". A "parameter signature" is the number, type and order of the parameters using in the double max ( double d1, double d2 ) { ... return doubleReturnValue; } int main () { int n1, n2; cout << max ( 5, 4); cout << max ( n1, n2); cout << max (3.14, 0.43434535); cout << max (3.14, 5); // should call max (double, double) When using an array as an parameter, it has a special case. void f1 ( int arr[] , int size) { int i; for (i = 0 ; i < size ; i++ ) arr[i] = 0; } int main () { int array1 [10]; int sz1 = 10; int array2 [100]; int sz2 = 100; f1 ( array1 , sz1 ); f1 ( array2 , sz2 ); A function may or may not modify the parameters values specified in the call. The difference occurs based on the type of variable used in the call and a specification indicated by the programmer. Are changes to parameters made in a function reflected back to the calling code? This difference is called - pass-by-value parameters vs. - pass-by-reference parameters Changes made in a function to a pass-by-value parameter are NOT reflected back to the calling code. Changes made in a function to a pass-by-reference parameter ARE reflected back to the calling code. void f2 ( int p1 ) { cout << p1 << endl; p1 = p1 + 10; cout << p1 << endl; } int main () { int number1 = 25; f2 ( number1 ); cout << number1 << endl; int arr3[50]; arr3[7] = 15; f2 ( arr3[7] ); } Output from the above program 25 35 25 Arrays are always pass-by-reference How to use pass-by-reference for non-arrays void foo(int & p) { cout << p << endl; p=1; cout << p << endl; } int main() { int a; a = 7; cout << a << endl; foo(a); cout << a << endl; } Output from the above code: 7 7 1 1