Lesson 12: Functions
In this lesson you will learn:
- how to use functions
- how to declare your own functions
- what prototypes are
- declaring functions
Q: What happened to the blonde tap dancer?
A: She slipped off and fell down the drain.
This part should be quite simple compared to the last part.
Functions are easy to use; they allow complicated programs
to be broken up into small blocks, each of which is easier
to write, read, and maintain. We have already encountered
the function main and made use of printf from the standard
library. Now let's look at writing and using our own functions.
Functions have a basic structure. Their format is:
return_data_type function_name (arguments, arguments)
data_type_declarations of arguments;
A function can be thought of as a mini-program, where a
group of statements are executed when the function is called.
A function is CALLED (or INVOKED) when you need to branch
off from the program flow and execute the group of statements
within that function. Once the statements in the function
are executed, program flow resumes from the place where
you called your function.
Functions have 5 main features:
The RETURN TYPE is the data type of the RETURN VALUE of
The NAME is required as an identifier to the function, so
that the computer knows which function is called. Naming
of functions follows the same set of rules as the naming
Functions can take ARGUMENTS - a function might need extra
information for it to work. Arguments are optional.
The function BODY is surrounded by curly brackets and contains
the statements of the function.
The RETURN VALUE is the value that is passed back to the
main program. Functions exit whenever you return a value.
This is what a function definition might look like:
int squareNumber(int a)
int b = a*a;
squareNumber is the name of this function. Because an integer
is returned, the int keyword must be placed before the function
If the function does not return a value, we put the void
keyword before the function name.
This function has one argument, which is of the type int.
If you have arguments, you must put variable declarations
in the round brackets.
The function body consists of 2 statements. The first,
sees an int variable b declared and assigned a*a, i.e. a
squared. The second statement uses the return keyword to
pass the value of b back into the main program, hence exiting
Within the program, one might write:
x = squareNumber(5);
This would assign 25 to x. We say that 5 is passed as an
argument to the function squareNumber.
The variables within the squareNumber function are LOCAL
VARIABLES - when the function exits, variables a and b are
deleted from memory.
void printAverage(int x, int y,
int z); /* the function declaration */
int a, b,
3 integers separated by spaces: ");
%d %d", &a, &b, &c);
b, c); /* the function call */
0; /* exit main function */
void printAverage(int x,
int y, int z)
= (float) (x + y + z) / 3;
average value of %d, %d and %d is %f\n", x, y, z, average);
It's common practice to place the function definition underneath
main - if you're editing main most of the time,
you wouldn't want to scroll too far down the page to get
You can put the function definition above it if you wanted.
But if you place it underneath main, make sure you put the
function declaration above main - see the
example. This is because the computer won't know if the
function exists if you call it without it being declared
in the first place. It's the same with variables: you can
assign anything to x unless you've declared x beforehand.
The function declaration is a single statement consisting
of the function header - don't forget the semi colon at
Notice that in the function call I had to pass three arguments
to match the three arguments in the function definition.
And the variable names in the function's argument section
didn't have to match the variable names in the function
call. The most important thing is that the data types had
And notice my use of the void keyword for the printAverage
function, since no value is returned.
back to top go
to next lesson