Reading:
Functions
A function, procedure or sub-routine are all names for a self-contained section of code, like a mini-program with optionally it's own inputs and outputs. Functions are useful for encapsulating code that can be called from multiple locations in the same program (so that it does not need to be re-written in multiple places). They can also be for structurally laying out of a program (via top down development,) or just to reduce the cognitive load with respect to some code -- one can stop thinking about what the individual statements in the function are doing and just think in terms of the whole function. It allows ones focus to zoom out of the code and keep more of the program in ones mind while programming. In general it is a good practice to use functions more often and there is rarely any downside. Grammar for a function definition:
The purpose of a function definition is to define completely the function,
it's inputs (the parameters to the function), output(s) (given by the return
type) and the internal code that processes the inputs (the statements within
the Grammar for a function prototype:
The purpose of a prototype is define a function defined either in another file or below the function attempting to use that function (in which case the prototype should be defined above the function attempting to use the function.) It contains just enough information to inform the compiler of how the function should be used (i.e. its inputs and outputs.) ReturnThe return keyword causes a function to exit immediately, giving a function it's value if a function has a non-void return type. The return keyword has the following grammar:
The expression can be omitted for functions with a void return type (i.e. functions that return no value, see below for more information.) Otherwise the expression must evaluate to the same kind of data that the function is expected to return (although some data-types can be converted to other data types, such as integer to floating point and visa-versa, this would still usually need to be done explicitly with a type-cast (to be discussed later.) Examples:
The 'void' TypeThe Example:
Function ParametersA functions parameters are defined as a comma separated list of full variable declarations (including the type) one for each variable, thus if two integer variables are required, they should be specified separately. Thus:
In this example both the More Examples:
"Calling" a FunctionOnce a function is "defined" or "prototyped", you can use it elsewhere in your
program by using the name of the function followed by the data you want to
send to it inside of parenthesis (
Thus when Call By Value
By default when a function is called, any character, integer or floating point values (and the not-yet discussed structures) used as parameters to the function are passed by value, i.e. the value is copied to the functions parameter variable. Thus if you use such variables in the call, that variable is not modified by the calling function.
In the above, the value is sent to foo, but not any reference to the variable,
thus if foo modifies it's local variable Call By ReferenceWe use call by reference when using the Strings (character arrays) and arrays of other data are often passed by
reference (address). To make a function that will take integer or float
references, we would need to discuss pointers, which is a topic for a later
time, but for now understand that you pass a non-pointer variable by reference
using the ampersand ( Again only non-array character, integer or floating point values (and structures) require the ampersand operator to make them pass by reference, arrays or strings (which are arrays of characters,) already pass by reference by default. Example:
Visibility of Functions In A ProgramThe word "visibility", when used in the context of a programming language, describes what is known about in other areas of a program. Usually we care mostly about variable visibility (or scope) since we may want to reuse the names of variables in different functions, but functions themselves have different visibility depending on where they are defined. If a function is defined in one file, then it is not visible to function in a different file, unless a prototype for the function is defined above the function that attempts to use it. Also if a function is defined after/below a function that would like to use it, then a prototype for the function must be made above the function that wants to use the function. If a function is defined before/above a function Examples:
In the above example if the function Variable ScopeVariable scope refers to the "visibility" of a variable within the program. The various scopes are: global
static global
local
block-local
static local
ReviewThings you should definately know:
|