C Programming Functions Explained
C Programming Functions Explained
Course
Produced by:
Maroua BEN BELGACEM
University of Monastir
Higher Institute of Computer Science of Mahdia
1 LCE/LCS
AU. 2019/2020
Plan
1 Basic concepts
4 The paintings
5 Strings
6 The functions
2
Chapter 6:
The FUNCTIONS
Modular programming
6
DEFINITION
The arguments of a function are called its parameters in the C language. The name of
the function is followed by the list of parameters and their types.
Il faut bien faire la différence entre les paramètres au moment de la définitiond’une
function and the parameters that will be given when calling a function. The
Parameters specified when defining a function are referred to as parameters.
formulas. The parameters passed to the function when the function is called are
called effective parameters. A formal parameter is a local variable, known
only within the function for which this parameter is defined. The
Formal and effective parameters must correspond in number and type; by
against the names may differ.
DEFINITION
NB:
The specification of the return value type is optional. If not specified
not the return type, the default type is int.
A function may not contain any parameters. In this case, we can declare
the list of parameters as void or simply as ().
8
DEFINITION
Example:
#include
void welcome (void) void main(void)
{ { char c;
Welcome to this fantastic program printf("Do you want to continue in");
which proves to you the power English or in French (E/F)
of the modularity. scanf("%c", &c);
} if (c == 'E')
void welcome(void) welcome();
{ else
printf("Welcome to this wonderful"); welcome();
program that shows you the
power of modularity.
}
The functions welcome() and bienvenue() have neither parameters nor return values.
However, let us note the use of the void type and the pair of parentheses for the call.
of these functions. The fact that there is no return type makes the use of return
superfluous. 9
DEFINITION
Note:
In principle, the order of definitions in the program text does not have an impact.
role, but each function must be declared or defined before it is called.
10
DECLARATION OF FUNCTIONS
In C, each function must be declared before it can be used. The declaration
inform the compiler of the types of the parameters and the result of the function.
With the help of this data, the compiler can check whether the number and type of
the parameters of a function are correct.
If in the program text the function is defined before its first call,
she does not need to be declared.
The declaration of a function is done through a prototype of the function which indicates
only the type of data transmitted and received by the function.
Syntax:
11
DECLARATION OF FUNCTIONS
Statement situation:
The local declaration of a function is made at the beginning of the definition block.
function, before variable declarations. The function thus declared is not known.
that of the function in which it is declared. One can also use a
global declaration of a function before any function definition.
12
DECLARATION OF FUNCTIONS
Example of local declaration:
#include <stdio.h>
#include <math.h>
void main (void)
{
double f (double x);
float x, y;
printf ("x = ");
scanf("%f", &x);
while (x != 1000)
{
y = f(x);
f(%f) = %f
print("x = ");
scanf("%f", &x);
}
}
double f(double x)
return ((sin(x) + log(x))/(exp(x) + 2));
}
13
DECLARATION OF FUNCTIONS
Example of a global declaration:
Include
#include
double f (double x);
void main (void)
{
float x, y;
printf ("x = ");
scanf ("%f", &x);
while (x != 1000)
{
y = f(x);
f(%f) = %f
printf ("x = ");
scanf("%f", &x);
}
}
double f (double x)
{
return ((sin(x) + log(x))/(exp(x) + 2)); 14
}
DECLARATION OF FUNCTIONS
We can distinguish the different types of function declarations such as
suit
Local declaration: A function can be declared locally within the function
who calls it (before the variable declaration). It is then available at this
fonction.
Remarks:
16
CALL OF FUNCTIONS
17
CALL OF FUNCTIONS
Passing by value:
The pass-by-value parameter mode does not allow for modification of the
content of variables used as actual parameters of a calling function.
18
CALLING FUNCTIONS
Pass by value
Let's look at two examples of the behavior of value transmission.
Example 1:
Let's consider the following program:
#include <stdio.h>
void display(int n)
{
printf("%d \n", n);
}
void main(void)
int n = 3;
display(n);
}
23
CALLING FUNCTIONS
Passing through addresses:
It seems that the only interaction a function can have with the outside is
is the return of its result (or the use of global variables). But in fact, thanks to
With pointers, it is possible to act remotely. All you need to do is to send not the
value of a variable but its address in memory (that is, a pointer to this)
variable). The called function can then modify the content of this location.
memory (and thus the content of the variable itself).
24
EXERCISE
Statement:
Write a function that allows converting a lowercase character (if it is) to
uppercase. Use this function to convert a text to uppercase.
25
EXERCISE
Solution :
26
27