Program Development Styles and Basics of C 5
Thus, in short, we can say that each function must hide some details.
The refinement decides what a particular function performs. It also decides a function's pre and
post conditions, that is, what data the function takes and what output it will provide. The data
used in these functions must be exactly declared. The data that can be used in a function is of
five types.
1) Input parameters: These parameters are utilized by the function. The function cannot change
the value of the variables permanently if values are passed only by method 'pass by value.'
2) Output parameters: These parameters hold the values of results calculated. These values can
be returned to the main function. 'Call by reference' can be used to avoid restriction of returning
one value at a time.
3) Inout parameters: The parameters are used for both the purposes, that is, input as well as
output. The function uses the initial value of the variable (parameter) and modifies it. As the
same variable is used for both the purposes, the value is changed. In C, 'call by address' can
be used to do this. In C++ call by address and reference can be applied to do this.
4) Local variables: These variables are declared inside the function body and their scope is
limited to the same function body. When a function is invoked, the local variables come into
existence and as soon as the execution is completed they are destroyed. Their life is subject to
execution of the function.
5) Global variables: These variables are global in nature and can be accessed from anywhere in
the program including sub-programs. These variables are insecure because their value can
be changed by any function. Sometimes due to use of global variables a program gives
unwanted secondary results. If the global and local variables are declared with the same
name, a more complex situation may arise. If variables with the same name are declared wi th
local and global declarations, the local variable gets first priority. To avoid such problems,
avoid using global variables. Preferably, the variable can be declared as a constant.
1.4 PROBLEM SOLVING TECHNIQUES
There are three ways to represent the logical steps for finding the solution to a given problem.
1) Algorithm
2) Flowchart
3) Pseudocode
In an algorithm, description of steps for a given problem is provided. Here stress is given on text. A
flowchart represents solution to a given problem graphically Pictorial representation of the logical
steps is a flowchart. Another way to express the solution to a given problem is by means of
pseudo code.
1.5 ALGORITHM
Algorithm is a very popular technique used to obtain solution for a given problem. An algorithm
is defined as a finite set of steps which provide a chain of actions for solving a definite nature of
problem. Each step in the algorithm should be well defined. This step will enable the reader to
translate each step into a program. Gradual procedure for solving a problem is illustrated in this
section.
An algorithm is a well-organized, pre-arranged, and defined textual computational module that
receives some value or set of values as input and provides a single or a set of values as output. These
well-defined computational steps are arranged in a sequence, which processes the given input into
output. Writing precise description of the algorithm using an easy language is most essential for
understanding the algorithm. An algorithm is said to be accurate and truthful only when it provides
the exact required output. The lengthy procedure is sub-divided into smaller parts, which makes it
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.117.212