Skip to content

Calling Functions

At this point, you may wonder why do we even need pointers in the first place. After all, we can always directly access the value of a variable. This works for both retrieval and assignment. It seems that having a pointer to a variable is redundant.

The purpose of pointers will be apparent in the next section when we use it in conjunction with functions.

Motivation

The main motivation is that there are many libraries available. Calling the functions defined in them will simplify our task by a lot as opposed to rewriting the function on our own. Furthermore, the implementation in the libraries are typically more efficient than our own implementation. For instance, some functions in the math library is shown below.

Function Parameters1 Result Comment
abs(x) int int Absolute value
ceil(x) double double Ceiling
cos(x) double (radians) double Cosine
exp(x) double double ex
fabs(x) double double Absolute value for double
floor(x) double double Floor
log(x) double double Natural logarithm
log10(x) double double Logarithm base 10
pow(x,y) double, double double xy
sin(x) double (radians) double Sine
sqrt(x) double double Square root
tan(x) double (radians) double Tangent

Compiling with <math.h>

To use the math functions, you need to do both of the following.

  1. Include the library with #include <math.h>
  2. Compile your program with -lm option (i.e., gcc -lm main.c) in sunfire.

Function Call

Syntax

<function name>(<argument>, <argument>, ...)

NOTE:

  • There can be one or more arguments.
  • The number of arguments must match the number of parameters1.
    • Exception is made for certain way of defining a function that we will not go into.
    • This is called variadic function and an example is scanf and printf.

We can try calling a few of the functions from the math library above.

MathFunctions.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <math.h>

int main(void) {
   int   x, y;
   float val;

   printf("Enter x and y: ");
   scanf("%d %d", &x, &y);
   printf("pow(%d, %d) = %f\n", x, y, pow(x,y));

   printf("Enter value: ");
   scanf("%f", &val);
   printf("sqrt(%f) = %f\n", val, sqrt(val));

   return 0;
}

Now, you may wonder why does the code pow(x,y) at line 10 works? Isn't pow supposed to take in double as parameters? This is actually due to implicit type conversion. The relevant part is reproduced below.

Implicit Type Conversion

Unfortunately, for the primitive data types above, C allows for implicit type conversion. As such, examples is hard to come by.

However, passing a float or double into a parameter of type int will generate warning. Unfortunately, it is still not an error. This can be seen in the attempt to pass double to abs below.

MathAbsType.c
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <stdio.h>
#include <math.h>

int main(void) {
  double x;

  printf("Enter a negative (double) value: ");
  scanf("%lf", &x);

  printf("|x| = %d\n", abs(x));

  return 0;
}

On the other hand, note that the number of arguments must match the number of parameters for both pow and sqrt. You can try the following two codes to get compilation error.

Function Prototype

So how do we know whether how many arguments to pass to a function and what type to pass? The table above shows a summary of the functions available in C math library. Notice that at a glance, it lets you know all you need to know on how to call the function.

Take the function pow for instance. You can see that it accepts two parameters x and y. You can also see that the type of both should be double. Lastly, the result is also a double.

This information can actually be summarised in a single line as function prototype. The function prototype for pow is shown below:

double pow(double x, double y)

The list of function prototypes are typically available in the header file and not the program file. Header file has the same name as the program file but with a different extension, .h instead of .c. In the case of the math library, the implementation (i.e., code) is in the file math.c and the header is in the file math.h. The intention is that by reading only the header file, you should know how to use the library without actually reading the exact implementation. As such, the function name should clearly indicate what the function is doing.


  1. Parameters are what is described by the function and arguments are what is given to the function during a function call. In practice, both are treated as if they are equivalent. In such cases, usually the parameters are called "formal parameters" and the arguments are called "actual parameters". But the use of parameters and arguments are short and preferrable. In particular, we can later mention about "pass the arguments to parameters".