Functions Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences National Cheng Kung University Last updated: 12 October 2004 Chapter 5
Functions Definition Act on data and return a value Two categories User-defined Built-in Declare and define #include a file with prototypes Put all prototypes in the beginning of your code Call the prototype right before use it Three bad reasons to do so
Function prototypes Example Legal but not good to include names of parameters Ex5-1
Defining the function Header Body Syntax
Variables Local variables Ex5-2 Global variables Ex5-3 Dangerous but necessary in C A good alternative in C++ Scope
Some good habits to design a function Short Carry out a single, easily understood task Be explicit Don’t use functions as parameters to functions Local variables are local variables Ex5-5
Overloading functions Function polymorphism Ex5-8