Lesson 8.1. Basic syntax of C functions

Introduction

Sometimes called a subroutine, module or procedure, a function is a group of instructions that performs a given task. Every program in C consists of at least one function: main(). The idea of functions is to avoid repeating the same piece of code at different places in the program. We write this code in a function and we call this function each time it is needed. In the previous lessons, we have already used some functions:

When a function is used, it is a call to a function. The following code is a call to the print function:

printf ("Hello");

Syntax

Consider, for the example, the following function max() which returns the larger of the two integers between a and b:

int max(int a, int b);

The syntax of this function is illustrated below:

Syntax of a C function

Let's detail this syntax:

Diagram of the max() function that returns the greater of two integers

Prototype and implementation

Writing a function is done in two parts:

Here is an example of the structure of a complete program with the implementation of a function :

#include <stdio.h>

// Prototype of the function
int max (int a, int b);

// Main function
int main(void)
{
  // Call the max function
  printf ("The greater is %d\n", max(12, 7));
  return 0;
}

// Implementation of the function
int max(int a, int b)
{
  if (a>b) return a; else return b;
}

Exercises

Exercise 1

We provide two functions addition() and subtraction() which return the sum and subtraction of parameters a and b respectively:

// Return the sum of a and b
int addition (int a,int b) {
  return a+b;
}
// Subtract b from a and return the result
int subtraction (int a,int b) {
  return a-b;
}

Using these functions as models, complete the program with

Function addition       : x + y = 12
Function subtraction   : x - y = 4
Function multiplication : x * y = 32

Exercise 2

Write a program that contains a function int square (int n) that calculates the square of the integer n. Test your function with the following main program:

int main(void) {
  int x=8;
  printf("%d*%d = %d\n", x, x, carre(x));
  return 0;
}

Exercise 3

Write a program that contains a function char parity(unsigned int n) that returns:

Test the function with a main program that asks the user to enter an integer before displaying whether it is even or odd.

Enter an integer: 15
15 is even.

The function even() should not display anything, but return 0 or 1 to the main program.

Quiz

In C, a function allows

Check Bravo! There are many advantages to using C functions. Try again...

How many parameters does the following function have:

int My_function (float x, unsigned short Val);
Check Bravo! The parameters of the function are in the parentheses. Try again...

The prototype of a function ...

Check Bravo! Prototypes are essential to write properly structured code. Try again...

The prototype of a function is located ...

Check Bravo! The prototypes must be placed before the first call of the function, thus before the main program. Try again...

The implementation of a function ...

Check Bravo! The implementation, as its name indicates, contains the code of the function. Try again...

The implementation of a function is placed ...

Check Bravo! The implementation of a function is placed after the main program. Try again...

See also


Last update : 11/20/2022