Reading Assignment: K.N. King Sections 7.1, 7.2, 7.5 Sections 16.5, 18.2, 20.1
Reading Assignment: K.N. King Sections 7.1, 7.2, 7.5 Sections 16.5, 18.2, 20.1
K.N. King
141
Functions
142
143
and
The function declaration provides all the information that is required to use the
function.
Good Style: Every function should be declared or defined before it is used.
144
145
Function Declarationa
type-name functionName ( parameters ) ;
Function Definition
type-name functionName ( parameters )
declarations
statements
A function definition has the same form as a function declaration except that the body
of code that implements the function is supplied.
Variables, types and constants declared within a function are local to the function a
The variables local to a function are created at the instant a function is called, exist
until the function returns, at which point they are destroyed.
147
Function Parameters
The parameters of a function are a comma-separated list of declarations of
the form
Example:
type-name identifer
int K, double X, short A[]
148
149
150
...
I = 17 ;
testFunc( 7, 14.5 ) ;
14.5
testFunc( K, Y + Z ) ;
126.55
testFunc( J - K , Y*Y ) ;
14
9.61
151
An array parameter is declared like an array, except that the size of the array can be
omitted.
Example:
The function cant determine the size of an array argumenta so the size of the array
must be passed as an additional argument to the function.
Even is the size of an array parameter is specified (e.g. B in the example above), C
allows a compatible array of any size to be passed as the corresponding argument.
If the parameter is a multidimensional array, the size in the first dimension may be
omitted, but all the size in all other dimensions must be specified.
...
/* Counting the number of negative values in an array */
int count negatives(const double A[], const int aSize )
int count = 0 , J ;
if ( A[ J ]
for ( J = 0 ; J
aSize ; J++ )
0.0 )
count++ ;
return count ;
...
xCount = count negatives( xArray, 1000 ) ;
153
Function example
int power( int x, int n );
...
main()
int i = 2, j = 10, k;
...
k = power( i, j );
...
...
int power( int x, int n )
int result = 1;
while ( n--
> 0)
result = result * x;
return result;
154
return statement
return expression ;
More on Scopes in C
Items declared in a local scope or a local block scope are only visible in that scope.
Each source file introduces a file scope containing all the types, data and functions
declared in that source file. Items declared in a source file outside of a function are
visible to all functions declared in the file.
The extern declaration prefix can be used to share declarations across source files.
The static declaration prefix can be used to limit the scope of a globally declared item
to the source file in which it occurs
Good Style: Use extern only when there is no other alternative for sharing variables
between files.
WARNING: Variables shared between files can lead to bad program structure and are
a major cause of errors.
156
Scope Example
int K;
void f( int K )
K = 1;
void g(void)
int K = 2;
157
if ( K
0)
int K;
K = 3;
K = 4;
void h(void)
K = 5;
The extern prefix on a declaration declares that the declared items exist in some other
file that is a part of the program
Normal usage: declare something in one source file and use extern in all other files
that need to access it
The static prefix on a declaration makes the declaration invisible outside of the file in
which it is declared. This can be used to hide declarations including function
declarations.
The static prefix also causes data items to have a lifetime that is the same as the
main program. Variables in a function declared with the static prefix retain their values
between calls of the function.
158
Scope example
/* File baz.c */
/* File foo.c */
char D ;
extern char D ;
int I ;
static int J;
void f(void )
159
..
int M ;
static int K ;
..
char S ;
..
extern char S ;
160
Preview: #include
systemFileName
#include
#include localFileName
#include <stdio.h>
#include "myInterface.h"
#include "C:\no\one\else\can\find\this\file.h"
161
Diagnostic functions
stdio.h
ctype.h
string.h
math.h
stdlib.h
stdarg.h
setjmp.h
Non-local jumps
signal.h
Signals
time.h
limits.h
float.h
<fileName.h>
162
/* File foo.h */
/* File foo.c */
#include foo.h
extern char D ;
char D ;
extern int I ;
static int J;
int I ;
163
char S ;
void f(void )
..
int M ;
static int K ;
..
extern char S ;
..
..