Functions
- Rules of functions
- Examples - writing a function, calling a function
- Function prototypes
- Visibility
- Call by value
- The stack
- auto, static and register
The Rules
- A function may accept as many parameters as it needs, or no parameters (like main)
- A function may return either one or no values
- Variables declared inside a function are only available to that function, unless explicitly passed to another function
Writing a Function - Example
- In below example the int is the TYPE of the value handed back.
- And here there are 3 parameters are accepted
Calling a Function - Example
- In main programme, the compiler knows these should be doubles & converts them automatically
- The 2nd line tells the compiler how print_table works
- Then the line no 9 says the function's return value is ignored- this is ok, if you don't hsve to use it
Prototypes
The (optional) line
is known as a prototype
- If the compiler meets a call to an unknown function it “guesses”
— Guess 1: the function returns an int, even if it doesn’t
— Guess 2: you have passed the correct number of parameters and made sure they are all of the correct type, even if you haven’t - The prototype provides the compiler with important information about the return type and parameters
Prototyping is Not Optional
- To achieve working programs the compiler is best given a prototype for each function called
- When calling a Standard Library function, #include the file specified in the help page(s) - this file will contain the prototype
- When calling one of your own functions, write a prototype by hand
Writing Prototypes
- Prototype:
- Function Header:
- The function prototyupe may optionally include variable names(which are ignored)
Take Care With Semicolons
- The prototype has a semicolon
- The Function Header has an open brace
- Don't counfudse the compiler by adding a semicolon into the function header
Examples Prototypes :
Example Calls
Call by Value
- When a function is called the parameters are copied - “call by value”
- The function is unable to change any variable passed as a parameter
- In the next chapter pointers are discussed which allow “call by reference”
- We have already had a sneak preview of this mechanism with scanf
Call by value Example:
In above example, the function was not able to alter "var" which is denoted in line no 8.
Also in line 14, the finction is able to alter "v". The value of "v" is mentioned in below result.
Result :
C and the Stack
- C uses a stack to store local variables (i.e. those declared in functions), it is also used when passing parameters to functions
- The calling function pushes the parameters
- The function is called
- The called function picks up the parameters
- The called function pushes its local variables
- When finished, the called function pops its local variables and jumps back to the calling function
- The calling function pops the parameters
- The return value is handled
Stack Example:
Result :
Storage
- C stores local variables on the stack
- Global variables may be declared. These are not stack based, but are placed in the data segment
- Special keywords exist to specify where local variables are stored:
auto - place on the stack (default)
static - place in the data segment
register - place in a CPU register
- Data may also be placed on the heap, this will be discussed in a later chapter
auto
- Local variables are automatically allocated on entry into, and automatically deallocated on exit from, a function
- These variables are therefore called “automatic”
- Initial value: random
- Initialisation: recommended
Example :
Here in line no 4, auto keyword is redundant.
static
- The static keyword instructs the compiler to place a variable into the data segment
- The data segment is permanent (static)
- A value left in a static in one call to a function will still be there at the next call
- Initial value: 0
- Initialisation: unnecessary if you like zeros
Example :
Here, "rows" is permanently located, but local to this function.
register
- The register keyword tells the compiler to place a variable into a CPU register (you cannot specify which)
- If a register is unavailable the request will be ignored
- Largely redundant with optimising compilers
- Initial value: random
- Initialisation: recommended
Global Variables
- Global variables are created by placing the declaration outside all functions
- They are placed in the data segment
- Initial value: 0
- Initialisation: unnecessary if you like zeros
Here variable "d" is global and available to all functions defined below it.