Tutorial nine – A look at functions in C

A function in C is a block of code that can be called from anywhere in your program. It has a name, accepts parameters and usually returns a single result. The various C libraries supply many functions. For example the math.h library has a pow function that lets you calculate powers. Here’s an example of using it.

#include <math.h> 
#include<stdio.h>
int main() {
   double x=5.7;
   double y=3;
   double answer= pow(x,y) ;
   printf("The result of %f to the power of %f is %f",x,y,answer) ;
   return 0;
 }

This outputs : The result of 5.700000 to the power of 3.000000 is 185.193000.

The definition of pow is

double pow(double x,double y)

The first double is the type of value that pow returns. This can be any type or even void.

The double x and double y are the input parameters.  These values are passed in to the pow function.

So a function is always defined by three parts which make up the functions signature.

  1. Return value type or void.
  2. Name of the function.
  3. Input parameters. These are optional but if present must include type and name.

It’s quite common to have functions that do something but don’t return anything or have any input parameters.

For instance in the Asteroids game, there are functions like MoveAsteroids();   This is defined as

void MoveAsteroids()

As there is no return value, the word void must be used and as there are no input parameters, the empty brackets are used.

Input parameters are always copied in

While you can pass in any type of variable, it’s important to remember that a copy is always made. This is known as pass-by-value. It means that if you pass in a variable inside the function, it does not change it in the outside world. There is a way to do this with pointers and we’ll come to that later.

Let’s create a function to calculate y= ax^2 + bx + c. We’ll have a, b and c with defined values and pass in x to the function.

#include <stdio.h>

double a, b, c;

double calc(double x) {
	return (a * x * x) + (b * x) + c;
}

int main() {
	a = 2.0;
	b = 4.5;
	c = 1.9;
	printf("ax^2+bx+c=%6.3f\n", calc(4.8));

}

That outputs:  ax^2+bx+c=69.580

Note this shows that a, b and c were declared before the function and so are in scope and can be used by the function. It’s also possible to change the values of a, b and c in the function, if you assign to them. This is considered not very good programming. In fact even reading external variables in this way is not very good.

Function Declarations

There are two places where you can declare a function.

  1. Early on in the program before the main() function or before anywhere it is called
  2. In a header file. WE’ll come to those later on.

Why would you declare a function before main() or before it is called? To use a function, the compiler must know the signature so it can verify that a call of that function is correct- the numbers and type of parameters passed in and the type returned (or void).

If you declare the full function including the body before it is called then there are no problems.

You might decide to move the calc() function in the example above to after where it is called  (in main()). If you just moved the function after main(), the compiler would be unhappy and would give you an error like Warning C4013 ‘calc’ undefined; assuming extern returning int calc D:\development\c\calc\calc.c 10 .

What you have to do is put in a single line declaration

#include <stdio.h>

double a, b, c;

double calc(double x);  // Declaration of calc

int main() {

	a = 2.0;
	b = 4.5;
	c = 1.9;
	printf("ax^2+bx+c=%6.3f\n", calc(4.8));
}

// definition of calc
double calc(double x) {
	return (a * x * x) + (b * x) + c;
}

Adding that declaration makes the compiler happy.

We’ll return to functions in the future with function pointers which are about as advanced as C gets. In the next tutorial, I’ll look at if and switch statements.

Link to previous tutorial.

Link to Next Tutorial

(Visited 239 times, 1 visits today)