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:

`printf()`

`scanf()`

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

function:

`printf ("Hello");`

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:

Let's detail this syntax:

**Return type**: is the type of the value returned by the function (int, float, char ...).**Function name**: is the name that will be used to call the function.**Parameters or arguments**: these are the arguments that will be passed to the function when it is called. The type of each parameter must be specified.

Writing a function is done in two parts:

**The prototype**informs the compiler about the name of the function and its use (arguments and returned type).**The implementation**contains the code of the function.

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;
}
```

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

- the prototype,
- the implementation
- the call of a new function
`multiplication()`

which returns the product of its two parameters:

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

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;
}
```

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

that returns:

`0`

if`n`

is an odd number ;`1`

if`n`

is an even number;

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. **

**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...

- Learn C programming
- Lesson 1.1. History of C programming language
- Lesson 1.2. First program
- Lesson 1.3. Compilation
- Lesson 1.4. Preprocessor directives
- Lesson 1.5. Which compiler to choose?
- Lesson 1.6. Flowchart
- Lesson 2.1. Types and variables
- Lesson 2.2. Integers
- Lesson 2.3. Floating point numbers
- Lesson 2.4. Characters
- Lesson 2.5. Variables initialization
- Lesson 2.6. Ariane flight 501
- Lesson 3.1. Arithmetic operators
- Lesson 3.2. modulo
- Lesson 3.3. Operators and types
- Lesson 3.4. Type casting
- Lesson 3.5. Bitwise operators
- Lesson 3.6. Bitwise operators details
- Lesson 3.7. Shifting operators
- Lesson 3.8. Assignment operators
- Lesson 3.9. Increment and decrement operators
- Lesson 3.10. Comparison operators
- Lesson 3.11. Logical operators
- Lesson 3.12. Operator precedence
- Lesson 4.1. printf
- Lesson 4.2. scanf
- Lesson 4.3. putchar
- Lesson 5.1. Conditional branching (if...else)
- Lesson 5.2. Nested if and indentation
- Lesson 5.3. Checking intervals
- Lesson 5.4. Ternary Operator ?:
- Lesson 5.5. switch..case statement
- Lesson 5.6. Break keyword in switch..case
- Lesson 6.1. do..while loop in C
- Lesson 6.2. While loop in C
- Lesson 6.3. For loop in C
- Lesson 6.4. How to choose the right loop in C ?
- Lesson 6.5. Exercices on loops in C
- Lesson 7.1. Bit masking
- Lesson 7.2. Bit masking, how to clear a bit in C?
- Lesson 7.3. Bit masking, how to set a bit in C?
- Lesson 7.4. Bit masking, how to toggle a bit in C?
- Lesson 7.5. Bit masking, how to check a single bit in C?
- Lesson 7.6. Overview of bit masking
- Lesson 8.2. Calling C functions
- Lesson 8.3. Void keyword
- Lesson 8.4. Return keyword
- Lesson 8.5. Variable scope
- Lesson 8.6. Global variables
- Lesson 8.7. Static variables
- Lesson 8.8. Random numbers in C
- Lesson 8.9. Mathematical functions in C
- Lesson 9.1. Syntax of C arrays
- Lesson 9.2. Array initialization
- Lesson 9.3. Multidimensional arrays
- Lesson 9.4. How C arrays are stored in memory?
- Lesson 9.5. Arrays and functions
- Lesson 9.6. Exercises on C arrays
- Lesson 10.1. Strings in C
- Lesson 10.2. Null terminated string
- Lesson 10.3. The string.h library
- Lesson 10.4. String and functions
- Lesson 11.1. Introduction to pointers in C
- Lesson 11.2. Syntax of C pointers
- Lesson 11.3. Dynamic memory allocation
- Lesson 11.4. Incrementing pointers
- Lesson 11.5. Pointers as function argument in C
- Lesson 12.1. Introduction to structures in C
- Lesson 12.2. Properties of structures in C
- Lesson 12.3. Structures and pointers
- Lesson 12.4. Structures and functions
- Lesson 13.1. Recursive functions in C
- Lesson 13.2. Depth of recursive functions
- Lesson 13.2. Mutual recursion
- Lesson 14.1. Additional exercises

Last update : 11/20/2022