Practical C Programming chapter 9
1. Scope and Class
all variables have two attributes: scope and class.
The scope of a variable is the area of the program in which the variable is valid.
global variable is valid everywhere, so its scope is the whole program
local variable has a scope that is limited to the block in which its is declared and cannot be accessed outside the block.
a block is a section of code enclosed in curly braces ({})
you can declare a local variable with the same name as a global variable. in the block where that local variable declared, the global variable was hidden
the class of a variable my be either permanent or temporary.
global variables are always permanent.
temporary variables are allocated from a section of memory called the stack at the beginning of the block
local variable are temporary unless they are declared static
Note: static has an entirely different meaning when used with global variables. It indicates that a variable is local to the current file.
Note: temporary variable are sometimes referred to as automatic variables because the space for them is allocated automatically. The qualifier auto can be used to denote a temporary variable.
Declaration Modifiers
Declared Scope Class Initialized
outside all blocks Global Permanent Once
static outside all block Global Permanent Once
inside a block Local Temporary Each time block is entered
static inside a block Local Permanent Once
2. Functions
Functions allow us to group commonly used code into a compact unit that can be used repeatedly
Each function should begin with a comment block containing the following:
Name
Name of the function
Description
Description of what the function does
Parameters
Description of each of the parameters to the function
Returns
Description of the return value of the function
addtional sections may be added such as file formats, references, or notes
return_type function_name(parameter_type parameter1, ...)
C uses a form of parameter passing called "Call by value"
Note: if no function type is declared, the type defaults to int.
the variable names are not required when declaring a function prototype; however, we use the longer version because it gives the programmer additional information
3. Functions with no parameters
K&R-style function declaration
The keyword void is used to indicate an empty parameter list.
void is also used to indicated that a function dose not return a value.
4. Structured programming
flow charts
top-down programming
bottom-up programming
structured programming
object-oriented design
structured programming: these techniques are ways of dividing up or structuring a program into small, well-defined functions.
the first step in programming is to decide what you are going to do. Next, decide how are going to structure your data.
Finally, the coding phase begins.
In actual practice, both top-down and bottom-up techniques are useful. A mostly top-down, partially bottom-up techniques results.
5. recursion
recursion occurs when a function calls itself directly or indirectly.
a recursion function must follow two basic rules:
a. it must have an ending point
b. it must make the problem simpler
1. Scope and Class
all variables have two attributes: scope and class.
The scope of a variable is the area of the program in which the variable is valid.
global variable is valid everywhere, so its scope is the whole program
local variable has a scope that is limited to the block in which its is declared and cannot be accessed outside the block.
a block is a section of code enclosed in curly braces ({})
you can declare a local variable with the same name as a global variable. in the block where that local variable declared, the global variable was hidden
the class of a variable my be either permanent or temporary.
global variables are always permanent.
temporary variables are allocated from a section of memory called the stack at the beginning of the block
local variable are temporary unless they are declared static
Note: static has an entirely different meaning when used with global variables. It indicates that a variable is local to the current file.
Note: temporary variable are sometimes referred to as automatic variables because the space for them is allocated automatically. The qualifier auto can be used to denote a temporary variable.
Declaration Modifiers
Declared Scope Class Initialized
outside all blocks Global Permanent Once
static outside all block Global Permanent Once
inside a block Local Temporary Each time block is entered
static inside a block Local Permanent Once
2. Functions
Functions allow us to group commonly used code into a compact unit that can be used repeatedly
Each function should begin with a comment block containing the following:
Name
Name of the function
Description
Description of what the function does
Parameters
Description of each of the parameters to the function
Returns
Description of the return value of the function
addtional sections may be added such as file formats, references, or notes
return_type function_name(parameter_type parameter1, ...)
C uses a form of parameter passing called "Call by value"
Note: if no function type is declared, the type defaults to int.
the variable names are not required when declaring a function prototype; however, we use the longer version because it gives the programmer additional information
3. Functions with no parameters
K&R-style function declaration
The keyword void is used to indicate an empty parameter list.
void is also used to indicated that a function dose not return a value.
4. Structured programming
flow charts
top-down programming
bottom-up programming
structured programming
object-oriented design
structured programming: these techniques are ways of dividing up or structuring a program into small, well-defined functions.
the first step in programming is to decide what you are going to do. Next, decide how are going to structure your data.
Finally, the coding phase begins.
int main() {
init();
solve_problems();
finish_up();
return 0;
}
start by writing the main function, it should be less than three pages long. If it grows longer, consider splitting it up into two smaller, simpler functions. After the main function is complete, you can start on the others.
In actual practice, both top-down and bottom-up techniques are useful. A mostly top-down, partially bottom-up techniques results.
5. recursion
recursion occurs when a function calls itself directly or indirectly.
a recursion function must follow two basic rules:
a. it must have an ending point
b. it must make the problem simpler