0% found this document useful (0 votes)
6 views27 pages

C Programming Functions Explained

This document presents a programming workshop focused on functions in the C language, addressing their definition, declaration, and calling. It explains the concepts of passing by value and by address, as well as the importance of modularity and local and global variables. Practical examples illustrate the use of functions and their impact on code clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

C Programming Functions Explained

This document presents a programming workshop focused on functions in the C language, addressing their definition, declaration, and calling. It explains the concepts of passing by value and by address, as well as the importance of modularity and local and global variables. Practical examples illustrate the use of functions and their impact on code clarity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Workshop Programming 1

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

2 Conditional control structures

3 Iterative control structures

4 The paintings

5 Strings

6 The functions
2
Chapter 6:
The FUNCTIONS
Modular programming

A long program is difficult to understand as a whole. It is therefore better to


split into small programs: a main program calls for sub-
programs that can themselves call upon subprograms from
main program or this one, and so on. This is the principle of refinement.
successive. Furthermore, some subprograms can be used in others.
programs, it is the principle of modularity. These principles are implemented in
C language thanks to functions.
In C language, we can distinguish between predefined functions of libraries (such as
like printf() or scanf()), delivered with the compiler and 'integrated' into the program
during the edition of links, and the functions that the programmer writes himself as
which part of the source text. We have already seen how to use the first ones. We
Let's therefore focus here on seconds.
DEFINITION

A function is a set of instructions structured in a block accessible via


a proper name called the name of the function.
Syntax:

The return value of a function is passed to the calling function via


the instruction: return
Syntax:
5
DEFINITION
This instruction allows:
the evaluation of the <expression>
the automatic conversion of the result of the expression to the type of the function
the return of the result
the ending of the function
Example: The function MAX is of type int and it needs two parameters.
typeint. The result of the function MAX can be integrated into other expressions.

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.

It is not necessary to specify the return type for a function.


which does not return a result. We can at least associate it with the type void as
type of result.

The type of the value returned by a function can be any type (a


basic type or even a structure.

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.

A function cannot return arrays or strings as a result.


characters or functions

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:

Example: the MAX function, its declaration is written as:

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.

Global declaration: A function can be declared globally at the beginning of the


program (behind the #include instructions). It is then available to all
the functions of the program.

Implicit declaration by definition: The function is automatically


available to all the functions that follow its definition.
15
DECLARATION OF FUNCTIONS

Remarks:

The main function 'main' does not need to be declared.


The variables declared in a block of instructions are only visible to
the inside of this block. They are said to be local variables to this block. Thus, no
other functions do not have access to these variables.

16
CALL OF FUNCTIONS

To call a function inside another function, we refer to


its identifier (name) possibly followed by the list of parameters.

At the time of execution, a call to a function results in branching


in the block associated with the function, the execution of the instructions of the block and return to

the instruction that follows the call.

When passing parameters to a function, two modes are distinguished.


to know
- Passage des paramètres par valeurs
Passing parameters by addresses

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.

The parameters of a function should be considered as local variables that


are automatically initialized by the values specified during a call.

Inside the function, we can therefore change the values of


parameters without influencing the original values in the calling functions.

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

Its execution displays 3 as expected.


19
CALL OF FUNCTIONS
Pass by value
Example 2:
Let's consider the following modified program:
#include <stdio.h>
void display(int n)
{
n = 4;
printf ("%d \n", n);
}
void main (void)
{
int n = 3;
poster (n);
printf ("%d \n", n);
}
Its execution displays 4 then 3. The display() function has therefore not changed the
value of n in the main function. 20
CALL OF FUNCTIONS
Global variable
A global variable is a variable declared before any function definition. The interest
Is it then known to all functions, so we can change its value in
chacuned’elle. The drawback is that one can also inadvertently change its value. It
it is therefore better to use passing by reference than a global variable. However, let's see
how it works.
Example 1: Let’s consider the following program
#include< stdio.h>
int n = 3;
void display (void)
{
The execution of this program displays
n = 4;
4 then 4. The function display() therefore has
printf("%d \n", n);
changed the value of n in the function
}
main.
void main (void)
{
display();
printf("%d \n", n);
}
21
CALL OF FUNCTIONS
Global variable
Note: The use of global variables in a program is allowed, but it
It is better to avoid using it. Indeed, it goes against the clarity of modularity.
When considering a subprogram, one should see at a glance all the
variables that are involved. This is the whole point of the arguments.
Example 2: Note that, in the case of the global variable above, the function
affiche() had no argument. Let's consider, in contrast, the following program:
#include< stdio.h>
int n = 3;
void display (int n) Its execution displays 4 then 3. The
{ declaration of the argument in the function
n = 4; display() considers it as a variable
printf ("%d \n", n); locale. The variable n in the body of this function
} corresponds to the last declared variable,
void main(void) so to the local variable, and not to the variable
{ global; this explains why the value of the
poster global variable, it has not changed.
printf ("%d \n", n);
}
22
CALL OF FUNCTIONS
Variable locale
The procedure itself may need variables that are of no interest for the
program in its entirety. We then talk about local variable.
Note: The name of a local variable is justified by the position of its declaration. It is
also justify because such a variable is not known to the program in its entirety but
only from the function (more generally from the block) in which it was declared. This
Highlight the importance of using local variables as much as possible: it is a protection.
additional against programming errors.

23
CALLING FUNCTIONS
Passing through addresses:

Passing parameters by value does not allow modifying a variable.


from the calling function.

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

You might also like