What are the uses of Arrays
in programming? Explain with the help of an example. Also explain how functions
and subroutines are different to each other.
Ans
Array in most programming langauges like C ,C++, Java etc
is a static , homogeneous and lineardata structure.
A data structure itself is a mathematical and logical
model to organise data.It clearly specifies the domain of the data that can be
stored in the structure and the collection of valid operations (functions) that
can be performed on that data. Array is a linear structure in the sense that
the data is organised sequentially(one after another in a contineous chunck of
memory).
It is a homogeneous data structure i.e. it contains
elements which are of the same data type.Thedata type of array can
be inbuilt like int , float ,char, double and even user
defined that is created using a structure or a class.
The main use of an array is to oragnise homogeneous data
together as a group to perform operations on data in a collective manner since
traversal and retrieval becomes easy.Whenever a homogeneous list(One
Dimendional or Multi-Dimensional)has to be implemented we make use of array.The
advantage of array lies in the fact that it uses indexed represenattion
enabling us to access any member element directly.
The example of practical use of array in progarmming will
be creation of index lists,
pointer arrays,matrix operations(transpose,addition
,subtraction,multiplication,inverse etc.).
Subroutines and Functions
When a program is more than a few hundred lines long, it
gets hard to follow. Fortran codes that solve real engineering problems often
have tens of thousands of lines. The only way to handle such big codes is to
use a modular approach and split the program into many separate smaller units
called subprograms.
A subprogram is a (small) piece of code that solves a
well-defined sub-problem. In a large program, one often has to solve the same
sub-problems with many different data. Instead of replicating code, these tasks
should be solved by subprograms. The same subprogram can be invoked many times
with different input data.
Fortran has two different types of subprograms, called functions
and subroutines.
Functions
Fortran functions are quite similar to mathematical
functions: They both take a set of input arguments (parameters) and return a
value of some type. In the preceding discussion we talked about user defined
subprograms. Fortran also has some built-in functions.
A simple example illustrates how to use a function:
X = cos(45.24)
Here cos is the cosine function. There are many built-in
functions in Fortran. Some of the most common are:
abs absolute value
sqrt square root
sin sine
cos cosine
atan arctangent
exp exponential (natural)
log logarithm (natural)
In general, a function always has a type. Most of the
built-in functions mentioned above, however, are generic. So in the example
above, pi and x could be either of type real or double precision. The compiler
would check the types and use the correct version of cos (real or double
precision). Unfortunately, Fortran is not really a polymorph language so in
general you have to be careful to match the types of your variables and your
functions!
Main differences between FUNCTIONS and SUBROTINES
A SUBROUTINE has no value associated with its name. All
outputs are defined in terms of arguments; there may be any number of outputs.
A SUBROUTINE is not called into action simply by writing
its name, since no value is associated with the name. Instead, we write a CALL
statement to bring it into operation; this specifies the arguments and results
in storing all the output values.
Since the output of a SUBROUTINE may b any combination of
the various types of values, there is no type associated with the name and
likewise no convention attached to the first letter of the name.
No comments:
Post a Comment