Need for user defined function
1. It facilitates top-down modular programming.
2. The length of a source program can be reduced by using functions at appropriate places.
3. It is easy to lacate and isolate a faulty function for further investigations.
4. A function may be used by many other program
Definition of function
A function definition, also known as function implementation shall include the following elements;
1.function name;
2.function type;
3.list of parameteres;
4.local variable decleration;
5.function statement;
6.a return statement;
General format
function_type function_name(parameter list)
{
local variable declaration;
executable statement1;
executable statement2;
.....
......
return statement;
}
Category of function
No arguments and no return values
Argumentd but no return values
Argumentd with return values
No arguments but returns a value
Function that returns multiple value
The scope visibility and lifetime of variables
Types of variables
1.Automatic
They created when the function is called and destroyed automatically when the function is exited.
They are private (local) to the function in which they are declared. So also called local variable.
It is assign to a variable by default if no storage class is specified.
2.External
They are both alive and active through out the program.
Also known as global variables.
They are declared outside of a function
3. Static
The value of the static variables presists until the end of the program.
Key word static can be used to declare the variable
Internal static variable are similar to automatic variable
Static variables can be used to retain values between function calls
4.Register
Variables kept in the machine's registers.
Variables that needs much faster access than that given by memory access.
Most compiler allow only int or char variables to be placed in the register.
Scope Visibility and Lifetime
Scope
The region of a program in which a variable is available for use.
Visibility
The Program's ability to access variable from the memory.
Lifetime
The lifetime of a variable is the duration of time in which a variable exists in the memory during execution.
Rules of use
1. The scope of a global variable is the entire program file.
2. The scope of a local variable begins at point of declaration and ends at the end of the block or function in which it is declared.
3. The scope of a formal function argument is its own function.
4. The lifetime of an auto variable declared in main is the entire program execution time, although its scope is oonly in the main function.
5. The life of an auto variable declared in a function ends when the function is exited.
6. A static local variable, although its scope is limited to its function, its lifetime extends till the end of program execution.
7. All variables have visibility in their scope, provided that are not declared again.
8. If a variable is redeclared within its scope again, it loses its visibility in the scope of the redeclared variable.
Wednesday, June 9, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment