Functions --------- Reading: Chap 5 0) Introduction - outline - function definition - using a function - include files - compilation and linking - parameter passing - variable scope and global variables A) Function Definition - continue our overview of C++ by looking at functions - similar to methods in Java with some exceptions return_type function_name(paramater_list) // similar to main, a function { declarations statements return result; } - e.g. int // return type squared(int x) // one parameter of int type { int temp; // local declaration - its scope is the function temp = x * x; return temp; // return value type must match declaration } B) Using a Function int squared(int); // function declaration/prototype/interface // no parameters, just types int main() { int y, z; std::cin >> y; // read value into y z = squared(y); // x is formal parameter, y is actual parameter std::cout << z << std::endl; } C) Include Files - common practice is to place function prototype in a "include" file - e.g. - squared.h is the include file (ends with .h) which contains int squared(int); - now squared.cc containes the function definition together with #include "squared.h" // function declaration included in the file // useful for type checking - squared.cc and squared.h together form a program unit - squared.cc doesn't contain the function declaration directly - the main function is written in a separate file - includes the squared.h file #include "squared.h" int main() { ... z = squared(y); // include directive includes function declaration } - however, program is spread in three files - must be compiled together to produce an executable program D) Compilation and Linking - describe what is compilation and linking - compilation converts pieces of source code to binary code - code that is understood by the machine - does type checking to reduce programming errors - linking combines different pieces of binary code - resulting binary (called an executable) is self sufficient - has all the code so that it can run gcc -c main.cc // produces main.o, -c compiles the file // main.o has code that requires squared // squared is not available! gcc -c squared.cc gcc main.o squared.o -o squaredprog // linking - combining compilation and linking gcc main.cc squared.cc -o squaredprog - advantage: one command - disadvantage: ?? (compilation of all files) - leads to Makefiles - file organization - declarations in .h files - implementation in .cc files - #include header files so that the compiler can do type checking E) Parameter Passing 1) Example - consider how parameters are passed to functions int f(int a, int b, int c) { ... } x = 5; y = 8; z = 3; w = f(x, y, z); int f(int a, int b, int c) { // copy x to a, y to b, z to c ... // throw away a, b, c } - think of parameter passing by copying - called "pass-by-value" 2) Pass-by value - implications int f(....) { a = 0; <---- what happens to the value of x? nothing! copy changes } - what if you want to change the value of X? - e.g. void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } int main() { int x = 3; int y = 4; swap(x, y); std::cout << x << " " << y << std::endl; } // what is wrong? 3) Pass-by-reference - replace void swap(int a, int b) - with void swap(int &a, int &b) - and the new definition behaves likes passing by reference - in depth explanation of how this works when we describe pointers F) Variable Scope and Global Variables - scope - the part of the program in which a variable is visible - scope lies between definition of variable and end of corresponding block int main() { int a = 3; int b = 4; swap(a, b); // 'a' here and 'a' in swap are different variables } - local variables - defined inside a function - scope within the function - global variables - defined outside any function - scope is all functions defined after the variable is defined - avoid such variables for passing information in programs - importance of scope will become obvious when we discuss memory management