Notes on Sections 4.4, 4.5, 4.6


  • Black box

  • Local variable

  • Global constant

  • Overloaded function name

  • Automatic type conversion


  • Variables, constant and otherwise, declared inside a function definition are considered local to that function. They can not be used elsewhere in the program.

  • Variables, constant and otherwise, declared before the first function definition are considered global and may be used and changed by any function.

Take Note

  • Functions should be written in such a way that the user needs only know what is expected as input and what to expect as output. The comments at the declaration should make this clear. In other words, someone should be able to cut and paste your function definition into their program, having no idea how the function works, and be able to use it in the main() (and in their own functions definitions) with no problems.

  • The parameters passed to a function are in essence local variables which come pre-initialized to the values passed to the function.

  • It is strongly recommended that non-constant variables be made local and that constants be defined globally. This is a matter of programming style, but is a very good safety rule. It is only a guideline, however, and you may find occasions in long, complex programs when a global variable is exactly what you need. (I found global variables to be expedient in exactly one problem last year.)

  • The same function name can receive multiple definitions, as long as the number and types of the inputs ( double, int, char, bool) will make it unambiguous which function is being called.

  • While overloading is allowed, it is not generally encouraged, except for closely related functions. For example, one might define two functions both called max which would choose the largest of their inputs. One definition could call for two inputs and the other could call for three inputs. These functions would need separate definitions. The number of inputs a function takes is set in stone.

  • In general, the compiler will check to see that the variables passed to a function match the allowed input types. The very useful exception is that variables (and constants) of type int can be passed to a function expecting a double.

Topic revision: r1 - 2015-09-20 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback