You are on page 1of 12

27/02/2018

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

WRITING A FUNCTION - EXAMPLE

2
27/02/2018

CALLING A FUNCTION - EXAMPLE

CALLING A FUNCTION - DISASTER!


|16|warning: implicit declaration of function 'print_table' [-Wimplicit-function-
declaration]|

// int print_table(double, double, double);

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

PROTOTYPING IS NOT OPTIONAL


• To achieve working programs the compiler is
best given a prototype for each function called
• When calling a Standard Library function,
#include the file specified in the help page(s) -
this file will contain the prototype
• When calling one of your own functions, write a
prototype by hand

4
27/02/2018

WRITING PROTOTYPES
• Prototype:

• Function header:

• The function prototype may optionally include variable


names (which are ignored)

TAKE CARE WITH SEMICOLONS


• The prototype has a semicolon

• The function header has an open brace

• Don’t confuse the compiler by adding a semicolon into the


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

CALL BY VALUE - EXAMPLE

15

C and the Stack


• C uses a stack to store local variables (i.e. those declared in functions), it
is also used when passing parameters to functions
1. The calling function pushes the parameters
2. The function is called
3. The called function picks up the parameters
4. The called function pushes its local variables
5. When finished, the called function pops its local
variables and jumps back to the calling function
6. The calling function pops the parameters
7. The return value is handled

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

You might also like