Skip to main content

Prototypes, Definitions and Calls

This section contains syntactical details regarding functions.

Shape of a function

In order to use functions we generally need to do three things:

  • declare the function
  • define the function
  • call the function

In programming there is a concept called declaration before use

In this case, before a program can use a function we must first know what we expect that function to be. This can be achieve one of two ways:

  • place the function definition of the function earlier in the file than the functions where we make the function call. This doesn't work well if we have multiple files

  • place the function prototypes of all functions at the top of the file. This allows us to write all functions in any order or even in other files.

Function declaration

A function is declared using a function prototype. A function prototype tells the user of the function everything they need to know to use the function. It consists of a single statement of the form:

returnType functionName(paramtype1 <optional name>, paramtype2 <optional name>, ...);

For example, here is the function prototype for our volumeOfSphere() function.

double volumeOfSphere(double radius);

We can also skip the names of the parameters in the parameter list. The prototype below works just as well as the one above.

double volumeOfSphere(double);

Function prototypes define expectations; this is what the function is called ; this is what you need to give the function to use it; this is what it will return to you when done.

Function definitions

Function definitions state what the functions do. This is essentially where to write our functions.

returntype functionName(parameterList)
{
...code to solve the problem goes here
}

For example:

double getRadius(int sphereNumber)
{
double radius;
printf("Please enter the radius of sphere # %d: ",sphereNumber);
scanf("%lf",&radius);
return radius;
}

Function call

To use a function, you call it by its name and pass in the required arguments. The general syntax is this

functionName(arguments);

Example to call the getRadius() function we would use:

getRadius(1);

The above is known as a function call. The function call evaluates to the return value of that function given those argument(s). We can store the results of the function call into a variable:

radius = getRadius(1);

or we can use the result directly:

printf("volume of sphere is: %.2lf\n",volumeOfSphere(radius));

Styling Conventions

Names of functions are written in lowerCamelCase just like variable names. Also similar to variable names, we want function names to be descriptive.

Documentation

In the course notes, we make use comments in the code to explain things and point out details that may be missed. However, if we were to be writing programs that were in production, we would not use comments the same way. We would use comments to explain the code to future programmers (who may be ourselves...) what our intentions are for this code. Thus, when we write professional programs, we want to always include documentation for this. To do this, we want to add a comment to each function stating the following:

  • What the function is suppose to do. This is a description of "what", not "how". For example, "This function calculates the volume of a sphere" is a "what" comment. "This function takes the multiplies 4.0/3 then multiplies that by PI then multiplies that radius _ radius _ radius" is a "how"... it is method used to calculate the volume. We do not want to put "how's" into the comments.
  • The parameters passed to the function and any assumptions about those parameters that the function will make. "This function is passed a positive value representing the radius of the sphere". the positive is the assumption... it states what this function is expecting
  • The return value of the function. "This function will return the volume of the sphere"