Functions

Location
  1. Courses

    /

  2. Quick C course

    /

  3. Functions

What is a function

A function is a named set of instructions. It may receive parameters (arguments) and return (produce) a result. Calling a function means executing it.

Defining a function

Here we define (create) a function returning the value 5 as an int:

int nameOfTheFunction() { return 5; }

Note the int indicating that the function returns a value of type int.

Here we call it:

nameOfTheFunction();

Here we call it and retrieve the value it returns:

int var = nameOfTheFunction(); // 'var' equals 5.

Here is an example printing the result the function returns:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h> // For 'printf'. // We define a function. int nameOfTheFunction() { return 5; } int main() { // We print the value returned by the call of the function. printf("%d\n", nameOfTheFunction()); return 0; }

Arguments

Let us define a function that returns the result of the multiplication of the two values of type int it receives in argument:

int mult(int a, int b) { return a * b; }

Let us use write a small program using that function:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h> int mult(int a, int b) { return a * b; } int main() { int var = 6; int result = mult(8, var); // 'result' equals 48. // We print the value of the variable 'result'. printf("%d\n", result); return 0; }

Returning nothing

A function may return nothing. In that case, it must start with 'void':

void func(float arg) { // Some instructions... }

Arguments are copies

When we give a variable as argument to a function, the function only receives a copy of that variable:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h> void setToFive(int arg) { arg = 5; } int main() { int var = 66; setToFive(var); // 'var' still equals 66. printf("%d\n", var); return 0; }

The scope of variables

Variables only exist within the braces they have been defined in.

Look at the following code:

1
2
3
4
5
6
7
// Here, the variable 'var' does not exist. void func() { int var; // Here, the variable 'var' exists. } // Here, the variable 'var' does not exist.

It is possible to use braces for the sole purpose of limiting the scope of a variable:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() { // Here, the variable 'var' does not exist. { int var; // Here, the variable 'var' exists. } // Here, 'var' does not exist, so we can use the name for an other variable. float var; return 0; }

Declaring functions

There is an error in the following code:

main.c

1
2
3
4
5
6
7
8
9
10
int main() { func(1.6, 8); // Error: 'func' does not exist here. return 0; } void func(float arg, int arg2) { }

The reason is that the function 'func' is defined after it is used, so the compiler is not aware it exists. We may solve that problem by declaring it first.

Here is the declaration of the function 'func' from above:

void func(float arg, int arg2);

Here is the code with the problem solved:

main.c

1
2
3
4
5
6
7
8
9
10
11
12
void func(float arg, int arg2); int main() { func(1.6, 8); return 0; } void func(float arg, int arg2) { }