Tutorial - Functions in C

Main Page
Helpful Links
Downloads
Tutorials
Grades
Syllabus
Contact Me

In this tutorial, we look at the use of functions in C.  Functions are used to wrap up pieces of code that we will reuse.  This way, once fully tested, as long as no changes are made, a function can be used freely.  Functions also make our code more readable by hiding low level details from higher level functions.  Functions can be written to accept or return different values.

Anatomy of a Function

return_type function_name( function_arguments){

   function_body;
   return return_value;
}

return_type -- This is any basic variable type.  We can use void if the function doesn't return anything.

function_name -- This is the name of the function.  As with variables, a descriptive name should be chosen.

function_arguments -- This is a list of the types and variables the function takes.  These pairs are seperated by commas.  An example would be something like (int distance, char ID).  If not passing any arguments, we can simply leave the parentheses empty or type void in.

function_body -- We place the instructions of the function into the curly braces.

return_value -- If a non-void return type was declared, we need to return a value of the same type.  A function can have multiple return statements.  Whenever a return statement is encountered, execution leaves the function.  If the function's return type is void, no return statement is needed.


Function Prototypes

   Before a function can be used, the compiler must be aware of it, and how to use it.  So we use something called function prototyping.  When we do this, we insert the function's prototype somewhere before the main function.  (Later, we'll you'll learn how to place them into a .h file)
   Here's the basic form of a function prototype:

return_type function_name (argument_type_list);

   For example, seeing a prototype like this:

int multiply (int, int);

tells the compiler that whenever it sees "multiply", it should know it's a function that accepts two integers and returns an integer.  If we didn't have this, the compiler wouldn't recognize "multiply" when we try to use it, and would give an error.  So you should put in function prototypes for all your functions.

 Input, Output, and Variable Scope

   Remember, all arguments we pass to our functions are only copied.  Any changes made to them within a function are lost within the function.  We will discuss how to modify them permanently in a later chapter (although we touched on this regarding scanf).  So for now, the only way to get output from a function is the return value.
   Actually, that's a white lie.  In truth, functions can see variables outside of themselves.  These are considered to be global to the function.  It can read from them, and also make modifications.  Unfortunately, this is usually very poor style, since it can be very unclear which function is modifying a variable.  So, I would discourage you from doing that.
   Variables declared within a function have a local scope.  This means they are only visible from within the function, and from sub-functions.  When execution leaves the function, local variables are lost and are said to have gone out of scope.