0% found this document useful (0 votes)
20 views1 page

Scientific Programming and Computer Architecture 22

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1

Scientific Programming and Computer Architecture

statements are valuable aids to debugging and indirectly useful as comments.


The definition of aitkenExtrapolate(), which is also in aitken.c, is shown below to give a complete
account of the source aitken.c.
1 double aitkenExtrapolate(double *seq1, double* seq2,
2 int len){
3 int n, i, j;
4 n = len/2;
5 if(len%2==0)
6 n--;
7 for(i=0; i < n; i++){
8 aitken(seq1, seq2, len-2*i);
9 for(j=0; j < len-2*(i+1); j++)
10 seq1[j] = seq2[j];
11 }
12 return (len%2==0)?seq1[1]:seq1[0];
13 }
We assume familiarity with the level of C that occurs in this function, although reading programs such as
this can be harder than writing them. Writing good for-loops is the heart of C programming, and there are two
nested for-loops in this program. Line 12 uses a conditional expression that may be less familiar. The conditional
expression (a<b)?c:d tests the condition a<b. If the condition is true, it evaluates to c but to d otherwise.

1.2.4  Declarations and definitions

Names introduced into a C program are for the most part names of either functions or variables. The names can be
introduced as either declarations or definitions.
Suppose a variable name is introduced using int x. When the compiler encounters that statement, it sets
aside a location in memory for an int and makes x the name of that memory location. This statement is a variable
definition, not merely a declaration, because it sets aside memory for x.
A declaration gives type information about a variable that is expected to be defined elsewhere. An example
of a variable declaration is a statement such as extern int x. When the compiler encounters such a statement, it
notes that x is a variable of type int that is expected to be defined in some other source file. It does not set aside
any location in memory. If it later encounters a statement such as x=x+2, it does not complain. However, it cannot
generate complete machine instructions to carry out that statement because it has no idea where x is defined and
what address it corresponds to. That information has to be supplied by the linker later.
Both the lines in the header file aitken.h are function declarations not definitions. When the compiler
encounters a declaration such as
void aitken(double *seq1, double *seq2, int len);
it notes that aitken() is the name of a function that takes three arguments, the first two of which are of type
double * and the last of which is an int, and returns nothing (void). We can omit seq1, seq2, and len from the
declaration. Such an omission would make the declaration difficult to read and understand for us, but it makes no
difference as far as the compiler is concerned. The compiler does nothing more than note the types of the arguments
(or parameters).
When the compiler later encounters a function call such as aitken(s, t, n), it first checks that s and t
are of type double * and n is of type int. If the check succeeds, the compiler will generate instructions to set up
the arguments and pass control to the function aitken(). If the function aitken() is defined in some other source
file, which it may well be, the compiler has no idea where in memory the code for aitken() is located. So it
cannot generate complete machine instructions for passing control. That job is the linker’s responsibility.
A function definition such as
void aitken(double *seq1, double *seq2, int len){
...

https://divakarvi.github.io/bk-spca/spca.html[20-1-2019 23:44:49]

You might also like