Invoking Functions in C

Let's go into detail about how to invoke a function in C language.

In this lesson we will see how to call or invoke a function in C language. When you invoke a function, you pass the values of the arguments to the function and, if the function returns a value, we can use the result of the function as part of an expression.

Function Call

A Function Call in C language consists of inserting the function name followed by the list of arguments separated by commas and enclosed in parentheses.

For example:

sum(x, y);
difference(10, 4);
print_message();
Note

The parentheses must always be present

When you invoke a function, the list of arguments must always be present even if empty.

For example, if we have a function that does not accept parameters, we must still insert the empty parentheses:

print_message();

Otherwise, what happens is that we get a valid statement but without any effect. Modern compilers, however, generate a warning to signal this problem:

/* ERROR: No effect */
print_message;

When you invoke a function that does not return any result, therefore a void function, it is enough to follow the function invocation with a semicolon obtaining, thus, a statement.

print_message();

Conversely, if a function returns a value, we can use the result of the function as part of an expression.

int result = sum(10, 20);

The value returned by the sum function will be assigned to the result variable.

A return value of a function can also be ignored. This is useful when the returned value is not necessary for the program.

sum(10, 20);

Obviously, in this example, it is quite strange to ignore the result of a sum, but in general, this technique can be useful in situations where the returned value is not necessary.

For example, the printf function returns the number of characters printed, but often it is not necessary to use this value. In the programs we have written so far, we have ignored the value returned by printf.

/* We take the number of characters printed */
int num_characters = printf("Hello, World!\n");

/* We ignore the returned value */
printf("Hello, World!\n");

Ignoring the return value of a function is not an error. However, situations might arise where we want to make explicit the fact that the returned value is not used. To do this it is necessary, simply, to perform the cast to void of the returned value.

/*
 * We explicitly ignore
 * the returned value
 */
(void) printf("Hello, World!\n");

Using this explicit cast can become heavy, because every time you ignore the return value of a function, it is necessary to write (void). For this reason, it is more common to ignore the return value without performing the cast. We will proceed in this way in the rest of the guide.

Recapping:

Definition

Function Call

In C language, to call or invoke a function it is necessary to write the function name followed by the list of arguments enclosed in parentheses and separated by commas.

function_name(arg1, arg2, arg3);

If the function does not accept arguments, it is necessary to insert the empty parentheses:

function_name();

If the function returns a value, we can use the result as part of an expression:

result_type result = function_name(arg1, arg2);

The value returned by a function can be ignored:

function_name(arg1, arg2);

Example

Let's see an example of function call and combinations of function calls with expressions.

Let's define a difference function that returns the difference of two floating point numbers:

double difference(double a, double b) {
    return a - b;
}

Then let's define square that returns the square of a floating point number:

double square(double a) {
    return a * a;
}

We can combine the calls to these two functions to create a program that calculates the distance between two points on a plane given their coordinates:

#include <stdio.h>
#include <math.h>

double difference(double a, double b) {
    return a - b;
}

double square(double a) {
    return a * a;
}

double distance(double x1, double y1, double x2, double y2) {
    double dx = difference(x2, x1);
    double dy = difference(y2, y1);
    return sqrt(square(dx) + square(dy));
}

int main() {
    double x1, y1, x2, y2;

    printf("Enter the coordinates of the first point: ");
    scanf("%lf %lf", &x1, &y1);

    printf("Enter the coordinates of the second point: ");
    scanf("%lf %lf", &x2, &y2);

    double d = distance(x1, y1, x2, y2);

    printf("The distance between the two points is: %f\n", d);

    return 0;
}

In this example, the distance function calculates the distance between two points (x1, y1) and (x2, y2) using the difference and square functions:

double distance(double x1, double y1, double x2, double y2) {
    double dx = difference(x2, x1);
    double dy = difference(y2, y1);
    return sqrt(square(dx) + square(dy));
}

In the function we also used the standard library function sqrt to calculate the square root of a number.

This example shows how function calls can be combined to create more complex programs.

Note

Compilation with the Mathematical library

To compile the example program, if we are on a Unix-like system such as Linux, it is necessary to add the -lm option to the compilation command:

gcc -o distance distance.c -lm

This is necessary because the sqrt function is defined in the mathematical library libm. By adding the -lm option, we tell the compiler to link the program with the mathematical library.

This is not necessary if we are compiling under Windows with the Visual Studio compiler.

In Summary

In this lesson we have seen what is the syntax to invoke a function in C language:

  • A function invocation is composed of the function name followed by the list of arguments separated by commas and enclosed in parentheses.
  • The parentheses must always be present, even if empty.
  • If the function returns a value, we can use the result as part of an expression.
  • The value returned by a function can be ignored.

We have also seen an example of how function calls can be combined to create more complex programs.

In the next lesson we will study an important aspect of functions in C: function declarations.