Lesson 18 - Learning
Goals
18.1 Learn the
Scope of C variables and procedures
18.2 Learn about
Function Prototypes and how to use them
''SCOPE'' OF C VARIABLES AND
PROCEDURES
- The ''scope '' of a
variable or procedure is the part of the code which can access
that variable/procedure.
- If a variable/procedure is
declared at the top-level in a source file, any statement below
the declaration can access it. This is called an external variable/procedure.
- If a variable is declared
inside a procedure, it can be accessed only inside the procedure
(below the declaration). This is called an automatic variable.
- If a variable is declared
inside a block (in braces { }), it can be accessed only inside
the block (below the declaration). This is also an automatic variable.
- Automatic declarations override
external declarations.
- Procedures can't be declared
within other procedures.
LONGEVITY OF C VARIABLES
- Automatic
variables are created when a procedure or block is entered, and
destroyed when the procedure or block is exited.
- External
variables are created when the program begins execution, and are
preserved through the entire program execution.
FUNCTIONS
IN FORTRAN :
! THIS PROGRAM COMPUTES THE CUBE OF ITS INPUT.
REAL FUNCTION CUBE(X)
IMPLICIT NONE
REAL :: X
CUBE = X * X * X
END FUNCTION CUBE
IN C :
/* This program computes the cube of its input */
real cube (x)
float x;
{
return (x * x * x);
}
SIMILARITIES :
- A function can return 1 value of specified type.
Function calls can be used in expressions
DIFFERENCES :
- FORTRAN uses special variable (same name as function)
to hold return value
- C uses return statement to specify return value
STATIC VS. AUTOMATIC VARIABLES
STATIC :
- A static variable has a fixed location, and retains
its value from one procedure call to the next.
- A static variable may be
initialized to a particular value at the start of the program.
- Variables declared outside
of all procedures are static.
- A variable declared inside
a procedure must have the keyword ''static'' added to make it
static.
AUTOMATIC :
- An automatic variable has
a transient location which disappears after a procedure terminates.
- An automatic variable may
be initialized to a particular value, but this initialization
occurs every time the procedure is called.
- A variable declared inside
a procedure is automatic unless declared static.
STATIC VS. AUTOMATIC VARIABLES
#include <stdio.h>
void ephemeral ( ), permanent ( );
main()
{
ephemeral ( );
permanent ( );
ephemeral ( ); /* New value */
permanent ( ); /* Keep old value */
printf ("\n");
}
void ephemeral ( )
{
int value = 0;
printf ("E: %d\t", value++);
}
void permanent ( )
{
static int value = 0;
printf ("P: %d\t", value++);
}
~~~~~~~~~~~~~~~~~~~~~~
Output :
E: 0 P: 0 E: 0 P: 1
PROTOTYPE DECLARATION
- If a variable/procedure is
declared at the top-level in a source file, any statement below
the declaration can access it.
- This means that a function
can't call a function that is ''below'' it in the file. In particular,
you can't have two functions call each other. It also means the
main( ) function has to be on the bottom.
- To get around this, use ''prototype''
declarations. A prototype tells the compiler what the function
call looks like without listing the code, which comes later.
- A prototype must give the
number of arguments, the type of each argument, and the type of
the value returned. It does not need to give names to the arguments.
PROTOTYPE EXAMPLE
#include <stdio.h>
/* Two Prototype Declarations */
float average (float, float);
void swap (float *, float *); /* Both arguments to swap are */
/* pointers to floats. */
main () {
float a, b, ave;
a = 3.396;
b = 5.2;
ave = average (a,b);
swap (&a,&b);
printf ("The average of %.2f and %.2f is %.2f\n",
a, b, ave); /* Print 2 sig. figures */
}
float average (float x, float y) {
return (x+y)/2;
}
void swap (float *x, float *y) {
float temp;
temp = *x;
*x = *y;
*y = temp;
}
~~~~~~~~~~~~~~~~~~~~~~
OUTPUT :
The average of 5.20 and 3.40 is 4.30
PASSING ARRAYS IN C
- Arrays are always passed
call-by-reference because call-by-value would require copying
an entire array.
- Procedure array variable
is a pointer to array in calling procedure.
- Dimension of input array
in procedure can be left unspecified, as in FORTRAN.
PASSING ARRAYS - EXAMPLES
IN FORTRAN :
PROGRAM ARRAYS
IMPLICIT NONE
INTEGER :: A(50), TOP, BIGEST
...
TOP = BIGEST (A, 50)
...
! RETURN LARGEST OF FIRST N INTS OF B
INTEGER FUNCTION BIGEST (B, N)
INTEGER B(*)
BIGEST = -9999
DO 10 I=1,N
10 IF (B(I).GT.BIGEST) BIGEST = B(I)
STOP
END PROGRAM ARRAYS
IN C :
main ( )
{
int a[50], top ;
...
top = bigest (a, 50) ;
...
}
/* Return largest of first n ints of b */
int bigest (int b[ ], int n)
{
int i, largest;
for ( i=0 , largest=-9999 ; i<n ; i+=1)
if (b[i] > largest) largest=b[i];
return (largest);
}
Go back to lecture menu
Go back to main page
Copyright © 1996 McGill University. All rights
reserved.