Professional Documents
Culture Documents
FUNCTIONS
FUNCTIONS
Rules of functions
Examples - writing a function, calling a function
Function prototypes
Visibility
Call by value
The stack
auto, static and register
1
27/02/2018
THE RULES
• A function may accept as many parameters as
it needs, or no parameters (like main)
• A function may return either one or no values
• Variables declared inside a function are only
available to that function, unless explicitly
passed to another function
2
27/02/2018
3
27/02/2018
PROTOTYPES
• The (optional) line
int print_table(double, double, double);
is known as a prototype
• If the compiler meets a call to an unknown function it
“guesses” –
• Guess 1: the function returns an int, even if it doesn’t
• Guess 2: you have passed the correct number of
parameters and made sure they are all of the correct type,
even if you haven’t
• The prototype provides the compiler with important
information about the return type and parameters
4
27/02/2018
WRITING PROTOTYPES
• Prototype:
• Function header:
10
5
27/02/2018
EXAMPLE PROTOTYPES
/* no parameters, int return value */
int get_integer(void);
/* no parameters, double return value */
double get_double(void);
/* no parameters, no return value */
void clear_screen(void);
/* three int parameters, int return value */
int day_of_year(int day, int month, int year);
/* three int parameters, long int return value */
long day_since_1_jan_1970(int, int, int);
/* parameter checking DISABLED, double return value */
double k_and_r_function();
/* short int parameter, (default) int return value */
transfer(short int s);
11
EXAMPLE CALLS
12
6
27/02/2018
RULES OF VISIBILITY
• C is a block structured language, variables may only be used
in functions declaring them
13
CALL BY VALUE
• When a function is called the parameters are copied - “call
by value”
• The function is unable to change any variable passed as a
parameter
• In the next chapter pointers are discussed which allow “call
by reference”
• We have already had a sneak preview of this mechanism
with scanf
14
7
27/02/2018
15
16
8
27/02/2018
Stack Example
17
Storage
C stores local variables on the stack
Global variables may be declared. These are not
stack based, but are placed in the data segment
Special keywords exist to specify where local
variables are stored:
auto place on the stack (default)
static place in the data segment
register place in a CPU register
Data may also be placed on the heap, this will be
discussed in a later chapter
18
9
27/02/2018
auto
Local variables are automatically allocated on entry
into, and automatically deallocated on exit from, a
function
These variables are therefore called “automatic”
Initial value: random
Initialisation: recommended
19
static
• The static keyword instructs the compiler to
place a variable into the data segment
• The data segment is permanent (static)
• A value left in a static in one call to a function
will still be there at the next call
• Initial value: 0
• Initialisation: unnecessary if you like zeros
20
10
27/02/2018
register
• The register keyword tells the compiler to place a variable
into a CPU register (you cannot specify which)
• If a register is unavailable the request will be ignored
• Largely redundant with optimising compilers Initial
value: random
• Initialisation: recommended
21
Global Variables
• Global variables are created by placing the
declaration outside all functions
• They are placed in the data segment
• Initial value: 0
• Initialisation: unnecessary if you like zeros
22
11
27/02/2018
QUESTIONS?
?
23
12