Lecture notes Friday 2/26/99
Functions
- program modules:
- construct program from small pieces
- modules in C are called functions
- advantages:
- "divide and conquer", best way to develop and maintain large programs
- software reusability, abstraction, hiding
- avoid repetition of code
- pre-packed functions:
- in C Standard Library
- e.g. math library
- function definition:
- to define your own functions
- syntax: return-value-type function-name(parameter-list) { declarations statements }
- return-value-type is the data type of the result returned to caller, e.g. int, void
- parameter-list is a comma-separated list of declarations of parameters received
- return statement
- functions cannot be defined inside other functions
- function call:
- arguments of correct number, type and order
- call by value, i.e. no side effects
- evaluates to return value
- function prototypes:
- like function definition without body, parameter names can be omitted
- to validate function calls, coercion of arguments
- often as header files
- recursion:
- when a function calls itself
- need base case
- scope:
- local variables: block scope, attention: may be hidden
- global variables and function names: file scope, attention: side effects
- storage duration:
- (automatic vs.) static: retain value when block is exited, initialized to zero
- register: suggests use of registers (high-speed hardware)