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

3 Python vs R Procedures and Functions

Uploaded by

Kamingu
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 views35 pages

3 Python vs R Procedures and Functions

Uploaded by

Kamingu
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
You are on page 1/ 35

Optimall series on Python vs.

R for Data Scientists (English)


Vol. 001, No. 003

Python vs R for Data Scientists : Procedures and Functions


Gradi L. Kamingu∗
July 13, 2023

Résumé
This paper presents the notions of procedures and functions, as well as their imple-
mentation using Python and R programming languages. In addition, the paper addresses
certain notions of functional programming, such as currying, presenting them in an in-
formative way. We emphasize the importance of understanding these concepts to develop
effective and well-structured programs. The paper also highlights the similarities and dif-
ferences between Python and R instructions, giving readers a comparative perspective.
Keywords : Procedure ; Function ; functional programming ; Python language ; R language.


Researcher in Optimization and MAchine Learning Laboratory (OPTIMALL), DR Congo.
E-mail : g.kamingu@unikin.ac.cd.
1 Introduction
As programs become more and more complex, it becomes more and more laborious to solve them
by directly implementing the corresponding algorithms. This is why it is useful to decompose the
problem into independent sub-problems and solve each sub-problem on its own. This is where the
notion of subroutine (or sub-algorithm) comes in, which is a powerful tool in programming (Knuth,
1997a). Indeed, a judicious use of the functions, in particular thanks to the structured programming
approach, can considerably reduce the development and maintenance costs of a complex program,
while improving its quality and its reliability. This is why the syntax of many programming languages
includes the possibility of writing and using subroutines.

While Alan Turing (1945) had already introduced the concept of subroutines in an article dis-
cussing design proposals for the NPL ACE computer, the fundamental idea of subroutines emerged
from the work of John Mauchly and Kathleen Antonelli on ENIAC (Dasgupta, 2014). However, the
formalization of this concept took place during a symposium held at Harvard in January 1947 (Mau-
chly, 1982). David Wheeler 1 , along with Maurice Wilkes and Stanley Gill, are generally credited with
the invention of subroutines around 1951 (called closed sub-routines). They also provided the initial
explanation on how to design software libraries (Wilkes et al., 1951; Wheeler, 1952).

The rest of the paper is structured as follows. We proceed to the presentation of the notion of
subroutine in the section 2, then present the procedures and functions in Python in the section 3, and
the procedures and functions in R in the section 4. Finally, we discuss the elements of comparison of
these two languages in the section 6 before concluding.

2 Subroutines
2.1 Notion of subroutine
The more complex the algorithm becomes, the more likely it is to perform the same processing,
or similar processing, in several places (Darmangeat, 2008). Hence the use of subroutines, also called
sub-programs (or sub-algorithms). It is like, for example, an algorithm that can repeat ten times the
calculation of the arithmetic mean of the ten real numbers or an algorithm that requires the entry of
the answer "Yes" or "No" twenty times for different questions.

To solve this kind of problem, it will be necessary to separate the repetitive processing from the
instructions that compose it in a separate module. This module has several advantages, including :
• The simplification of the complexity of the algorithms by avoiding unnecessary copies, which
involves calling subroutines or reproducing the same instructions.
• The possibility of dividing the work so that the realization of large algorithms can be carried
out autonomously for the benefit of a significant saving of time.
• The readability is ensured, that is to say that the algorithm being modular, the localization of
the errors is easy ; it suffices to make a single modification in the right place, for the modification
to be effective for the whole algorithm.

The body of the algorithm is therefore the main algorithm (program) or main procedure. Thus, there
are two types of algorithms : procedures and functions :
1. David John Wheeler (1927 – 2004), a British computer scientist, the first in the world to obtain a doctorate
in Computer Science (in 1951). At the time, most academics associated with computer science research had
advanced degrees in Mathematics.

68
• The procedures, which are subroutines executed by a calling algorithm, which generally generate
changes in the values of certain variables or return several values (instead of only one) or no
value on each call.
• The functions, which are subroutines that must return one and only one value each time they
are called to the calling algorithm.

In most programming languages, the declaration (and therefore the definition) of a subroutine usually
includes :
• a keyword (procedure or function) in the case of a subroutine in a language clearly distinguishing
the different forms of subroutine ;
• the subroutine identifier (name given by the programmer to the subroutine) ;
• the description of parameters indicating for each one (if there are parameters) :
• the parameter identifier (name),
• the (explicit or implicit) type of the parameter (in the case of a subroutine in a typed
language).

In the case of a function, the declaration of the subroutine also includes :


• the type of the subroutine (in the case of a typed language function), ie the type of its value to
return.
In the case of a function, the definition of the subroutine also includes :
• a body containing the code performing the operation assigned to this subroutine ;
• the assignment, in the body of the subroutine, of a result to the value to return from the function.

2.2 Procedures
The declaration of a procedure is done as follows :

Procedure procedure_name (par1 as par_type1, par2 as par_type2, ... , parn as par_typen)


# Declaration of local variables
Var variable1 as variable_type1
Var variable2 as variable_type2
···
Var variablen as variable_typen

# Body of the procedure


Sequence of instructions
End Procedure nom_procedure

By using par1, par2, ..., parn to represent the n parameters of the procedure and par_type1,
par_type2, . . . , par_typen to designate their types respective.

The execution (or the call) of a procedure can be triggered by using its identifier and, if necessary,
by providing its parameters, separated by commas. This action triggers the execution of the instruc-
tions that make up the procedure. When a procedure is called, the program interrupts its normal
flow of execution, executes the instructions specified in the procedure, and then returns to the calling
program to continue execution from the next instruction.

69
It is possible to have a procedure without any parameters, as shown in the following example :

Example 2.1. The following procedure is used to greet the user :

#The procedure
Procedure greeting()
Write “Hello !”
End Procedure greeting

#The main algorithm


Begin
greeting()
End

The above algorithm defines a procedure named greeting which displays the message “Hello !”. Then,
in the main algorithm, the procedure greeting() is called. So, when the algorithm is executed, it
displays the message “Hello !”.

Example 2.2. Still in the same context, we can modify the algorithm to ask the user for his first name.

#The procedure
Procedure greeting()
Write "Enter your first name :"
Read firstname
Write "Hello", firstname, " !"
End Procedure greeting

#The main algorithm


Begin
greeting()
End

The pseudocode above defines a procedure called greeting(). This procedure asks the user to enter
their first name, reads the value entered and then displays the message “Hello [first name] !” where
[first name] represents the first name entered by the user. In the main algorithm, the greeting pro-
cedure is called. So when the algorithm is run, it asks the user for their first name and then displays
the corresponding greeting.

Example 2.3. Let’s modify the previous algorithm so that the procedure greeting() takes one para-
meter. So,

#The procedure
Procedure greeting (firstname as string)
Write "Hello", firstname, " !"
End Procedure greeting

#The main algorithm


Begin
Write "Enter your first name :"
Read your_firstname

70
greeting(your_firstname)
End

The previous algorithm allows the user to enter their first name, then calls the greeting() procedure
with that first name as a parameter. The greeting() procedure then displays the message "Hello"
followed by the user’s first name, with an exclamation mark at the end.

Consider another example where the procedure has more than one parameter.

Example 2.4 (Multiplication table). Let’s assume we want to display a specific part of the multipli-
cation table of a given number (which we will call "base"), by displaying each multiplication within a
range of operations defined by two numbers (a starting number and an ending number). Here is the
corresponding algorithm :

# The multiplication table procedure


Procedure tableMulti (base as integer, starting as integer, ending as integer)
Write “Fragment of the multiplication table by”, base, “ :”
Write «——————————————————————»
n ← starting
While n ≤ ending Do
Write n, «x», base, «=», n × base
n←n+1
End Procedure tableMulti

# The Main algorithm


Begin
tableMulti(4, 1, 12)
End.

When the procedure tableMulti() is called with the parameters 4, 1 and 12, as in the call tableMulti(4,
1, 12) in the main algorithm, the algorithm desplays a specific fragment of the multiplication table
for the number 4. Specifically, it displays the operations of multiplying 1 to 12 by 4, highlighting the
corresponding results.

Note that if there are several parameters, as in algorithms, the arguments must be provided, when
calling the procedure, in the same order as that of the corresponding parameters (also separated by
commas). The first argument will be assigned to the first parameter, the second argument will be
to the second parameter, and so on, up to the n-th argument which will be assigned to the n-th
parameter.

2.3 Functions
The declaration of a function is done as follows :

Function function_name (par1 as par_type1, ... , parn as par_typen) as return_type


# Declaration of local variables
Var variable1 as variable_type1
Var variable2 as variable_type2
···
Var variablen as variable_typen

71
# Body of the function
Sequence of instructions
...
Return return_value
End Function function_name

By using par1, par2, ..., parn to designate the n parameters of the procedure and par_type1,
par_type2, . . . , par_typen to designate their types respective.

Since the function is mainly designed to return a value, it is therefore necessary to specify the type
of the function, which actually corresponds to the type of value_to_return. It’s called return_type

The execution of a function can be initiated by using its identifier and, if necessary, by supplying
its parameters. This action triggers the execution of the instructions that make up the function.

Example 2.5.

# Variable declaration
Var x, y, s as integer

# The function
Function sum (num1 as integer, num2 as integer) as integer
Var sum as integer
sum_result ← num1 + num2
Return sum_result
End Function sum

# The main algorithm


Begin
Write "Input the value of the first number"
Read num1
Write "Input the value of the second number"
Read num2
s ← sum(x, y)
Print "The sum of ", x ," and ", y, " is : ", s
End.

This algorithm performs a sum operation between two numbers entered by the user. It uses a function
called sum(), which takes two integer parameters and returns their sum. In the main algorithm, it
asks the user to enter two numbers, then calls the sum() function with those numbers as arguments.
The result of the function is then stored in a variable s. Finally, the algorithm displays the message
"The sum of x and y is : s".

The function differs from the procedure in its ability to be called by name in an expression or
assignment. As in the following example :

Example 2.6.

# Variable declaration
Var x, y, z, result as integer

72
# The function sum()
Function sum (num1 as integer, num2 as integer, num3 as integer) as integer
Var sum as integer
sum_result ← num1 + num2 + num3
Return sum_result
End Function sum

# The main algorithm


Begin
Write "Input the value of the first number"
Read x
Write "Input the value of the second number"
Read y
Write "Input the value of the third number"
Read z
result ← − sum(x, y, z) +22
Write "The result is ", result
End.

This algorithm declares the integer variables x, y, z and result. It defines a function sum() which takes
three integer parameters and returns the sum of these three numbers. In the main algorithm, it asks
the user to enter three numbers, then it calls the sum() function with those numbers as arguments
and stores the result in the result variable. Finally, it subtracts 22 from the result and displays the
final result.

2.4 Passing parameters of a subroutine


The exchanges of information between a procedure and the calling sub-algorithm are done through
parameters. It should be noted that parameters are named differently depending on their location in
the source code. We can distinguish the following :
• the formal parameters or dummy arguments , which are variables used in the subroutine ;
• the actual parameters, which are variables (or values) provided when calling the routine. Some
languages, like Perl 6, use the term parameter for formal parameter and argument for effective
parameter.

However, two main types of parameter passing are used, offering distinct uses : (i) pass by value ; and
(ii) pass by reference.

2.4.1 Pass by value


Pass by value, also known as pass by copy, involves creating a copy of the value of the actual
parameter and passing it to the subroutine or function. Modifications made to the formal parameters
do not affect the actual parameters.

From a syntax perspective, in pass by value, nothing is placed before the parameter when declaring
the subroutine.

73
Example 2.7.

#The procedure incrementer()


Procedure incrementer(value)
value ← value +1
End Procedure incrementer

#The main algorithm


Begin
Var x as integer
x←5
incrementer(x)
Write x #Displays 5, the value of x has not been modified.
End

2.4.2 Pass by reference


Pass by reference, also known as pass by address, involves transmitting the memory address of the
actual parameter to the subroutine or function. Modifications made to the formal parameters directly
affect the actual parameters.

From a syntax perspective, in passing by reference, the keyword "Var" ; similar to the one used
in variable declarations, is placed in front of the formal parameter during the declaration of the
subroutine.

Example 2.8.

#The procedure incrementer()


Procedure incrementer(Var value as integer)
value ← value +1
End Procedure incrementer

#The main algorithm


Begin
Var x as integer
x←5
incrementer(x)
Write x #Displays 6, the value of x has been modified by reference.
End

In the above pass-by-value algorithm, the procedure incrementer() receives a copy of the value of
the variable x, and when the value is modified inside the function, it does not affect not the original
value of x. Therefore, displaying x at the end returns the original value, which is 5. In contrast, in the
pass by given reference algorithm, the procedure incrementer() receives a reference to the variable
x, and when the value is modified inside the procedure, it directly modifies the value d origin of x. So
displaying x at the end returns the modified value, which is 6.

Note also that pass by value has the advantage of guaranteeing security and data protection, but
it can be slower due to the copying of data and the double consumption of memory space (however,
it is well suited for simple variables). On the other hand, the pass by reference offers the advantage
of a fast access to the data and a reduced memory occupation, because it uses addresses. However, it
can present data security risks and requires knowledge of how the data is physically implanted on the
machine.

74
2.5 Scope of a variable
Definition 2.1. The scope of a variable refers to the area where this variable is visible and accessible.

A variable can be declared in two separate places.

• A variable declared outside the body of a subroutine (i.e., in the declaration section of the main
algorithm) is called global variable or globally scoped variable. It is accessible from anywhere in
the algorithm, including from procedures and functions. It exists for the lifetime of the program.

Example 2.9. Consider the following algorithm :


#Declaration of a global variable
Var x as integer
x←4

#The routine with a local variable


Procedure add()
#Declaration of a local variable
sum ← sum + value #Usage of the global variable.
Var y as integer
y ← 12
Print x + y #Access to the global variable and the local variable.
End Procedure add

#The main algorithm


Begin
add()
End.

In the previous example, the x variable is a global variable that can be used both in the proce-
dure add() and in the main algorithm. The y variable is a local variable that is declared and
used only in the procedure add(). So in the procedure we can access both the global variable x
and the local variable y.

• A variable declared in a subroutine is called local variable or variable with local scope. It is only
accessible to the procedure in which it is defined and is not accessible to other procedures. The
lifetime of a local variable is limited to the execution of the procedure.

Example 2.10. Consider the following algorithm :


#Declaration of a global variable
Var x as integer
x←4

#The procedure with a local variable of the same name


Procedure display()
#Declaration of a local variable with the same name
Var x as integer
x ← 12
Write x #Access to the local variable

75
End Procedure display

#The main algorithm


Begin
x #Access to the global variable
End

In this example, we have both a global variable x and a local variable x in the procedure
display(). When we access the variable x inside the routine, we are referring to the local
variable of the same name. On the other hand, when we access the variable x outside the
procedure, we are referring to the global variable. Thus, using the same variable name creates
a distinction between the global variable and the local variable.

Remark 2.1. Global variables should be avoided for program maintenance.

2.6 Nested functions


Definition 2.2. A nested function, also called internal function or local function, is a function defined
inside another function.

It can be called only by the main algorithm or by other nested subroutines, directly or indirectly,
in the same main algorithm.

Example 2.11. Let’s take the example of a function that calculates the triple of a number. This program
can be written in a simple way using an operation of multiplication by 3, or it can be formulated as
follows :

Function outer (x as integer) as integer


Function inner (y as integer) as integer
Return x × y
End inner
Return inner(3)
End outer

The function inner() takes an argument x of type integer and also returns an integer. Inside this
function we find another function called inner() which takes an argument y of type integer and re-
turns an integer. The function inner() simply returns the multiplication of x and y.

Then, inside the function outer(), we use the Return instruction to return the result of calling the
function inner() with the argument 3. Hence, the function outer() returns the result of multiplying
x by 3.

2.7 Recursive functions


Definition 2.3. A recursive subroutine is a subroutine that calls itself in its definition.

Recursion provides the ability to define an infinite set of objects using a finite statement. Similarly, a
finite recursive program can describe an infinite number of computations, even if it contains no explicit
loops (Wirth, 1976).

Example 2.12. Suppose we want to write a function that calculates the factorial of an integer n. As a
reminder, the factorial of a number, denoted n!, is calculated as follows :

76
n! = n × (n − 1) × (n − 2) × · · · × 2 × 1. (1)
In other words :

n! = n × (n − 1)!. (2)
This function can still be defined equivalently as follows :
(
1 si n = 0;
n! = (3)
n × (n − 1)! si n > 0.
This definition says that if n is zero, the result is 1. Otherwise, if n is greater than zero, the result
is n multiplied by the factorial of (n − 1). Hence the following algorithm :

Function facto (n as integer) as integer


If n = 0 then
Return 1
Else
Return n× facto(n − 1)
End Function facto

The function facto() takes an integer parameter n and also returns an integer. It uses a conditional
structure to determine if n equals zero. If so, that means we reach the base case or the termination
case, because the factorial of zero is defined as equal to 1, in other words, the recursion chain is broken.
Without its presence, the algorithm cannot terminate.

When n is not equal to zero, this indicates that we are in the general case or the propagation
case. In this case, we need to perform the recursive computation. The function returns the result of
the multiplication of n by the recursive call of the function facto() with the parameter n − 1. This
approach reduces the initial problem to a smaller one by recursively computing the factorial of n − 1
until reaching the base case.

In summary, a recursive function is defined by one or more base cases, where the function produces
a direct result without repeating itself, as well as one or more propagation cases, where the function
calls itself to solve a problem smaller. However, neither of these cases constitutes a complete definition
in itself. Therefore, the algorithm design of a recursive function requires specifying both the base cases
and the propagation cases to fully define its behavior.

Consider another illustration : Euclidean Algorithm, one of the oldest known algorithms (Knuth,
1997b), is described in Book VII (Proposition 1-3) of Euclid’s Elements, written around 300 BC. AD
and presented in the form of anthypheresis (Euclide, 1994).

Example 2.13 (Euclidean algorithm). A common formulation of this algorithm, presented in the work
of Cormen et al. (2004), is as follows : the GCD (Greatest Common Divisor) of two integers a and
b can be calculated using the relation gcd(a, b) = gcd(b, r) , where r is the remainder of the Euclidean
division of a by b, i.e. r = a mod b.
Thus, Euclidean algorithm can be rewritten as follows :
• If b = 0, the algorithm terminates and returns the value a : this is the base case.
• Otherwise, the algorithm calculates the remainder r of the Euclidean division of a by b, then
repeats the process replacing a by b and b by r (i.e. a ← b and b ← r). We then apply the relation
gcd(a, b) = gcd(b, r) : this is the case of propagation.

77
Function gcd(a, b as integers) as integer
If b = 0 then
Return a
Else
Return gcd(b, a mod b)
End Function gcd

2.8 Anonymous functions


Definition 2.4. A anonymous function, also called literal function, lambda abstraction, lambda func-
tion, lambda expression, lambda block or disposable function (Davies, 2016) is a function definition
that is not linked to an identifier.

Anonymous functions have their origin in the lambda calculus, a mathematical system developed
by Alonzo Church in 1936 (Fernandez, 2009). In this system, all functions are anonymous, which means
that they do not have explicit names assigned to them. Alonzo Church, an American mathematician,
computer scientist, logician and philosopher, introduced the concept of anonymous functions as part
of his pioneering work in the field of computability theory before the advent of electronic computers.

By the way, the terms "lambda abstraction", "lambda function", and "lambda expression" refer to
the notation for the function abstraction in the lambda calculus, where the usual function f (x) = M
would be written (λx.M ) (M is an expression that uses x).

The declaration of an anonymous function is done as follows :

f_anonymous ← Function (par1 as par_type1, ... , parn as par_typen) as function_type :


Return Expression
End Function

Where Expression represents the expression, i.e. the value or the calculation, whose result should be
returned.

Example 2.14.

f ← Function (x as real) real :


Return x2
End Function

For simplicity of writing, we can write the general format of anonymous functions as follows :

f_anonymous ← Function (par1 as par_type1, ... , parn as par_typen) as function_type :


Expression

Example 2.15. The anonymous function in the example 2.14 can be rewritten in a simplified way as
follows :

f ← Function (x as real) as real : x2

78
The use of anonymous functions is a matter of style. This is never the only way to solve a problem,
because each anonymous function could instead be defined as a named function and called by name.

For the purposes of this document, we will only use anonymous functions to perform currying.

However, it is important to note that anonymous functions are limited in complexity and functio-
nality. They are generally used for simple operations and are not intended to replace functions defined
by name when advanced functionality is required.

Anonymous functions can be used to contain functionality that does not need to be named and
possibly for short term use. Notable examples are currying 2 .

Definition 2.5 (Currying). Currying is a technique that consists of transforming a function with several
parameters into a sequence of functions that each take a single parameter.

Example 2.16. Consider the following function to calculate the sum of three real numbers :

Function sum(x, y, z as real) as real


Return x + y + z
End Function sum

Using currying, the program can be rewritten as follows :

Function sum_c(x as real) as real


Return Function (y as real) as real :
Return Function (z as real) as real :
x+y+z
End Function
End Function
End Function sum_c

The function sum_c() takes a parameter x of real type and returns an anonymous function taking a
parameter y of real type. This anonymous function in turn returns another anonymous function taking
a parameter z of real type. Inside this last anonymous function, the sum x + y + z is calculated and
returned.

The main idea is that each call of the function sum_c() with a parameter x returns a function
which will take the next parameter (y), then this function will return another function which will take
the last parameter (z) and calculate the final sum.

The main algorithm therefore accepts the following calls :

#Main algorithm
Begin
2. The term “currying” is a concept used by logician and mathematician Haskell B. Curry (1980), although
Moses Schönfinkel developed it six years earlier. It is also known as "Schönfinkelisation" (Heim and Kratzer,
1998). The precise origin of the term is unclear, but it is mentioned that Christopher Strachey may have coined
it in his 1967 lecture notes (Turner, 1997). However, the word itself does not appear in these notes. John C.
Reynolds (1972) then defined Currying in an article, without claiming invention of the term. The principle of
currying also dates back to the mathematical work of Frege in 1893 (Quine, 1967; Turner, 1997)

79
# Calling parameters one by one
step1 = sum_c(5)
step2 = step1(-3)
result = step2(5.0)
Write result
End

The function sum_c() takes a parameter x of real type and returns an anonymous function taking
a parameter y of real type. This anonymous function in turn returns another anonymous function
taking a parameter z of type real. Inside this last anonymous function, the sum x + y + z is calculated
and returned. The main idea is that each call of the function sum_c() with a parameter x returns a
function which will take the next parameter (y), then this function will return another function which
will take the last parameter (z) and will calculate the final sum.

This main program can be explained as follows :


• We use the first step step1 = sum_c(5) to create a new function step1 passing it the argument
5. This function step1 corresponds to the first step of currying, where x is set to 5 and we expect
the argument y.
• Next, we use the second step step2 = step1(-3) to create a new function step2 passing it the
argument −3. This function step2 corresponds to the second step of currying, where y is set to
−3 and we expect the argument z.
• Finally, we use the last step result = step2(5.0) to call the function step2 with the argument
5.0. This calculates the sum of x (which is 5), y (which is −3) and z (which is 5.0) and stores
the result in the variable result.
• When the value of result is displayed, the program will display 7.0.

The main algorithm therefore accepts the following calls :

#Main algorithm
Begin
# Calling the parameter sequence at once
sum_c(5)(-3)(5.0)
End
Here’s an explanation of the previous main program :

• sum_c(5) corresponds to the initial call to the function sum_c(5) with argument x equal to 5.
This function takes a parameter y and returns another anonymous function

Function (z as real) as real : 5 + y + z

• sum_c(5)(-3)=(sum_c(5))(-3) corresponds to the call of the anonymous function returned in


the previous step with the argument y equals −3. This returns a second anonymous function

Function (z as real) as real : 5 + (−3) + z.

This function takes a z parameter and returns the sum of 5, -3 and z.


• sum_c(5)(-3)(5.0) =((sum_c(5))(-3))(5.0) corresponds to the call of the second anony-
mous function returned at previous step with argument z equal to 5.0. This last function sums
the three numbers x, y and z (5 + (−3) + 5.0) and returns the result.

80
3 Procedures and functions in Python
3.1 Notion of procedure and function in Python
In Python, the term "function" is used to encompass both strict functions (which return a value)
and procedures (which do not). Python uses the same def statement to define both types. Here is the
general syntax for defining a function in Python :
def function_name ( par1 , par2 , ... , parn ) :
# Declaration of local variables
variable1 : variable1_value
variable2 : variable2_value
# ...
variablen : variablen_value
# Body of the subroutine
# Sequence of instructions
# ...

# Optionally , return a value with the return statement ( for a function


, not applicable to procedures )

return return_value

With :
• def function_name(par1, par2 ..., parn) : is the declaration of the function, where we
must specify the name of the function, the parameters with their types and the type of value
back.
• Sequence of instructions : is where the code that will be executed when the function is
called will be written.
• return return_value : is the return statement that returns the specified value as the result of
the subroutine.
It is possible to choose any identifier to name a subroutine in Python, provided that you do not use
the reserved words of the language (see the paper by Kamingu (2022)). Also, it is recommended not
to use any special or accented characters (except the underscore “_”), and to adopt the snake_case
style of case, just like for variable names in Python.

3.2 Procedures without parameters in Python


In Python, it is possible to create a procedure without parameters. This can be illustrated with
the following example :

Example 3.1.

In [14]: def greeting () :


print ( " Hello ! " )

It is important to note that, just like for loops and conditionals, the indentation of the body of a
procedure is mandatory.

If we later call the procedure greeting() in the script, the message Hello! will be displayed.

81
However, introducing a parameter into the procedure results in an error, as shown in the following
example :
Example 3.2.

In [17]: greeting ( " Hello ! " )


------------------------------------------------------------
TypeError Traceback ( most recent call last )
< ipython - input -4 -873 f4b694044 > in < module >
----> 1 greeting ( " Hello ! " )

TypeError : greeting () takes 0 positional arguments but 1 was


given

Similar to the previous section, we can modify the program to include the user’s first name.
Example 3.3.

In [20]: # The procedure


def greeting () :
first_name = input ( " Input your first name : " )
print ( " Hello " , first_name , " ! " )
...

In [28]: # Calling the procedure


greeting ()

Input your first name : Pierre


Hello Pierre !

3.3 Procedures with parameters in Python


The parameters are used in the same way as in the algorithm. If the procedure has multiple para-
meters, the arguments must be provided in the corresponding order when calling the procedure, also
separated by commas.

The version of the example 3.3 with one parameter can be written as follows :
Example 3.4.

In [29]: # The procedure


def greeting ( first_name ) :
print ( " Hello " , first_name , " ! " )
...

In [32]: # Calling the procedure


print ( " Input your first name : " )
your_first_name = input ()
greeting ( your_first_name )

Let’s go back to the example of writing a program displaying a fragment of the multiplication table
that we mentioned earlier (example 2.4). Here is the corresponding script :

82
Example 3.5 (Python multiplication table).

In [1]: def tableMulti ( base , starting , ending ) :


print ( " Fragment of the multiplication table by " , base , " : " )
print ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " )

n = starting
while n <= ending :
print (n , " x " , base , " = " , n * base )
n += 1

In [2]: tableMulti (4 , 1 , 12)

After calling the procedure tableMulti(4, 1, 12), the result obtained is the following :

Fragment of the multiplication table by 4 :


----------------------------------------------------
1 x 4 = 4
2 x 4 = 8
3 x 4 = 12
4 x 4 = 16
5 x 4 = 20
6 x 4 = 24
7 x 4 = 28
8 x 4 = 32
9 x 4 = 36
10 x 4 = 40
11 x 4 = 44
12 x 4 = 48

Remark 3.1. In the example script 3.5, we used the assignment statement n += 1 instead of n = n+1.
This is because the += notation is a combined assignment operator in Python. It allows updating the
value of a variable by adding another value to the existing one and storing the result in the same
variable. Thus, the expression x += y is equivalent to x = x + y, where x and y can be variables or
expressions, and Opt represents an arithmetic operation such as Opt ∈ {+, -, *, /, %}.

3.4 Functions in Python


As mentioned earlier, functions in Python are similar to procedures, except that they must return
a value.

Example 3.6. The following function can be used to calculate the sum of two numbers :

In [42]: def sum ( num_1 , num_2 ) :


sum_result = num_1 + num_2
return sum_result

83
3.5 Subroutine parameters in Python
3.5.1 Passing parameters in Python
In Python, parameters are passed by reference. This means that when an object is passed to a su-
broutine as a parameter, that object’s reference is passed rather than its value. Therefore, any changes
made to the object inside the subroutine will be reflected outside of it.

However, it is important to note that the behavior may vary depending on the type of object
passed as a parameter. Two cases can be distinguished :

1. Immutable objects (such as variables (integers, reals, strings, tuples 3 ) are passed by value. This
means that when an immutable object is passed as an argument, a copy of its value is created
inside the subroutine, any changes made to the object inside the subroutine will not affect the
original object outside the subroutine.

Example 3.7.

In [43]: # The incrementer function


def incrementer ( value ) :
value += 1

In [44]: # The main program


x = 5
incrementer ( x )
print ( x ) # Displays 5 , the value of x has not been
modified .

2. Mutable objects 4 (such as lists, dictionaries) are passed by reference. This means that when an
object modifiable as a parameter is passed as an argument, changes made to that object inside
the subroutine will be reflected in the original object outside the subroutine.

It is important to understand these differences when manipulating parameters in a Python


subroutine, as it can impact the behavior of the code.
A peculiarity of subroutines in Python is that it is not necessary to specify the type of the para-
meters passed, as long as the operations performed with these arguments are valid. Python is indeed
known as a “dynamically typed” language, that is to say that it recognizes the type of variables for
you at runtime.

Example 3.8.

In [45]: def times (x , y ) :


return x * y

In [46]: times (2 , 3)
Out [46]: 6

In [47]: times (3.1415 , 5.23)

3. This type of object will be covered in volume 001, number 004


4. These types of objects will also be covered in volume 001, issue 004

84
Out [47]: 16 .4 30 04 50 00 00 000 3

In [48]: times ( " pa " , 2)


Out [48]: ’ papa ’

The * operator in Python is able to process different data types such as integers, floating point num-
bers, strings, and lists. This means that our function times() can perform different tasks depending
on the types of the supplied arguments. However, it is important to be aware of this great flexibility
offered by Python, as it can sometimes lead to unexpected results in your programs.

It is generally preferable for each argument to have a specific, clearly defined type, rather than
allowing different types of input. For example, it is clearer and less prone to design errors if we define
that the function times() only works with integers, floating point numbers or strings specifically.
This helps ensure consistent results and avoid unwanted behavior. It is therefore recommended to pay
attention to the flexibility offered by Python and to specify the types of arguments expected in your
functions, in order to avoid potential problems and to make your code more robust and predictable.

The function times() can thus be modified so that x and y can receive at least one numeric
parameter.

Example 3.9.

In [50]: def times (x , y ) :


if isinstance (x , ( int , float ) ) or isinstance (y , ( int ,
float ) ) :
return x * y
else :
raise TypeError ( " At least one of the arguments must be
numeric . " )

In [51]: # Examples of calls to the function times ()


Out [51]: print ( times (2 , 3) )
6

In [52]: print ( times (3.1415 , 5.23) )


16 .4 30 04 50 00 00 00 03

In [53]: print ( times ( " pa " , 2) )


’ papa ’

In [54]: print ( times ( " pa " , " pa " ) )


------------------------------------------------------
TypeError Traceback ( most recent call last )
< ipython - input -15 -07 de78aeb39f > in < module >
----> 1 times ( " pa " , " pa " )

< ipython - input -13 - f37b48f62162 > in times (x , y )


3 return x * y
4 else :
----> 5 raise TypeError ( " At least one of the arguments
must be numeric . " )

85
TypeError : At least one of the arguments must be numeric .

In this version, we use the function isinstance() to check if x is an integer or a floating-point


number, and if y is an integer or a floating-point number. The function isinstance() returns True
if the specified object is of the specified type, otherwise it returns False. In our case, if at least one
of the parameters is numeric, the function performs the multiplication x * y and returns the result.
Otherwise, a TypeError exception is raised to indicate that at least one of the parameters must be
numeric. With this change, the function times() will accept numeric parameters and only perform
the multiplication if at least one of the parameters is numeric.

3.5.2 Default Arguments in Python


In the definition of a subroutine, it is possible (and often desirable) (Swinnen, 2012) to define a
default argument for each of the parameters. We thus obtain a subroutine which can be called with
only a part of the expected arguments.

Let’s go back to the example 3.3. We can customize the greeting as in the following example :

Example 3.10.

In [65]: def greeting ( first_name , message = " Hello " ) :


print ( message , first_name , " ! " )

In [66]: greeting ( " Paul " )


Hello Paul !

In [67]: greeting ( " Paul " , " Good Morning " )


Good Morning Paul !

It is important to place parameters without default values before parameters with default values in
the definition of a subroutine in Python. This is due to the mechanism of associating arguments with
parameters when calling the subroutine. Python associates arguments with parameters in the order
they are defined in the subroutine definition. By placing parameters without default values first, Py-
thon can associate them with the first arguments provided during the call. Then parameters with
default values can be associated with the remaining arguments, if provided. This approach ensures a
consistent mapping between arguments and parameters, making it easier to understand and use the
subroutine.

Here is an example to illustrate the importance of the order of the parameters :

def example(parameter1, parameter2 = “default value”, parameter3)

In this example, if we set the parameter parameter2 with a default value before parameter1,
it will cause a syntax error. Python won’t know how to map the arguments provided during the
subroutine call to the corresponding parameters. By respecting the order of the parameters without
default value before the parameters with default values, the association between the arguments and the
parameters is done correctly when calling the subroutine. It also ensures consistency and predictability
of subroutine behavior.

86
3.5.3 Arguments with labels in Python
In most programming languages, it is generally necessary to supply the arguments in the same
order as the corresponding parameters in the definition of a subroutine. However, Python offers greater
flexibility in this regard. When the subroutine’s parameters have default values, the subroutine can
be called by supplying the arguments in any order, provided that the names of the corresponding
parameters are explicitly specified.

Let’s go back to the example of writing a program that displays a fragment of the multiplication
table that we mentioned earlier. We can modify it in the following way :

Example 3.11 (Multiplication table in Python).

In [1]: def tableMulti ( base = 4 , starting = 1 , ending = 12) :


print ( " Fragment of the multiplication table by " , base , " : " )
print ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " )

n = starting
while n <= ending :
print (n , " x " , base , " = " , n * base )
n += 1

In [2]: tableMulti ( starting =3 , ending =14 , base =5)

After calling the procedure tableMulti(starting=3, ending=14, base=5), the result obtained is
the following :

Fragment of the multiplication table by 5 :


----------------------------------------------------
3 x 5 = 15
4 x 5 = 20
5 x 5 = 25
6 x 5 = 30
7 x 5 = 35
8 x 5 = 40
9 x 5 = 45
10 x 5 = 50
11 x 5 = 55
12 x 5 = 60
13 x 5 = 65
14 x 5 = 70
By using parameter names, we can specify the arguments in any order, which provides greater
flexibility when calling the subroutine.

3.6 Scope of a variable in Pyton


Here is the example of the variable with global scope :

Example 3.12 (Global variable in Python).

87
In [25]: # Declaration of a global variable
x = 5

In [26]: # The procedure with a local variable


def add () :
# Declaration of a local variable
y = 12
print ( x + y ) # Accessing the global variable and the
local variable

In [27]: # Calling the procedure


add ()

Here is the example of the variable with local scope :

Example 3.13 (Global variable en Python).

In [28]: # Declaration of a global variable


x = 4

In [29]: # The procedure with a local variable of the same name


def display () :
# Declaration of a local variable with the same name
x = 12
print ( x ) # Accessing the local variable

In [30]: # Calling the procedure


display ()
12

In [31]: print ( x ) # Accessing the global variable


Out [31]: 4

Remark 3.2. In Python, it is possible to define a subroutine capable of modifying a global variable
using the instruction global. This statement is used inside the subroutine definition to indicate which
variables should be treated as global variables. This allows these global variables to be accessed and
modified within the subroutine.

Example 3.14 (Example of using the instruction global).

In [35]: # Declaration of a global variable


x = 4

In [36]: # The function with a local variable of the same name


def display () :
global x # Declaration of the global variable
x = 12 # Changing the global variable
print ( x ) # Displays the modified global variable

In [37]: # Function call


display ()

88
12

In [38]: print ( x ) # Displays the modified global variable


Out [38]: 12
In the previous example, we want to use the global variable x rather than creating a new local
variable of the same name. So when we change the value of x inside the function using x = 12, we are
changing the global variable itself. When we print x at the end, we can see that the global variable
has been changed and the value is now 12.

3.7 Recursive functions in Python


The use of recursion is allowed in Python. Hence the facto() function and Euclidian algorithm
(the function gcd() ).

Example 3.15 (Function facto)).

In [1]: def facto ( n ) :


if n == 0:
return 1
else :
return n * facto ( n - 1)

In [2]: # Using the function facto ()


facto (10)
Out [2]: 3628800

Example 3.16 (Function of Euclidean algorithm).

In [3]: def gcd (a , b ) :


if b == 0:
return a
else :
return gcd (b , a % b )

In [4]: # Using the function gcd ()


gcd (245 , 3500)
Out [4]: 35

3.8 Anonymous functions in Python


In Python, anonymous functions are created using the keyword lambda, followed by function
parameters and an expression.
lambda arguments : expression

Example 3.17 (Simple example of an anonymous function in Python).

In [450]: f = lambda x : 2* x **2+4

In [451]: f (5)
Out [451]: 54

89
Example 3.18 (Currying in Python).

In [454]: def somme_c ( x ) :


def fonction ( y ) :
def fonction_interne ( z ) :
return x + y + z
return fonction_interne
return fonction

In [455]: somme_c (5) ( -3) (5.0)


Out [454]: 7.0

4 Procedures and functions in R


4.1 Notion of procedure and function in R
As in the Python language, the term function is used to refer to strict functions (which return
a value) and procedures (which do not). However, to define a function or a procedure, R uses the
same function statement to define both types. Here is the general syntax for defining a function in
Python :
name_function <- function ( par1 , par2 , ... , parn ) {
# Declaration of local variables
variable1 <- value_variable1
variable2 <- value_variable2
# ...
variablen <- value_variablen
# Body of the function
# Sequence of instructions
# ...

# Optionally , returning a value with the return statement ( for a


strict function or procedure )

return ( return_value )
}

With :
• function_name <- function(par1, par2 ..., parn) : is the declaration of the function,
where we must specify the name of the function, the parameters with their types and the
return value type.
• Sequence of instructions : is where the code that will be executed when the function is
called will be written.
• return return_value : is the return statement that returns the specified value as the result of
the function.
It is possible to choose any identifier to name a function in R, provided that you do not use the
reserved words of the language (see Kamingu (2022)). In addition, it is recommended not to use any
special or accented characters, and to adopt the camelCase style of case, just like for variable names
in R.

90
4.2 Procedures without parameters
In R, it is possible to create a procedure without parameters. This can be illustrated with the
following example :

Example 4.1.

greeting <- function () {


print ( " Hello ! " )
}
When the procedure is called, we get :
> greeting ()
[1] " Hello ! "

It is important to note that in R, just like for loops and conditionals, indenting the body of a function
or procedure is optional due to the use of braces.

If we later call the procedure greeting() or procedure in the script, the message Hello! will be
displayed.

However, if we introduce a parameter in the function or procedure, it may result in an error, as


the following example shows :

Example 4.2.

> greeting ( " Hello ! " )


Error in greeting ( " Hello ! " ) : unused argument ( " Hello ! " )

The previous program can be modified to include the user’s first name.

Example 4.3.

# The procedure
greeting <- function () {
firstName <- readline ( prompt = " Input your first name : " )
print ( paste ( " Hello " , firstName , " ! " ) )
}

When the procedure is called, we get the following result :


> greeting ()
Input your first name : Gradi
[1] " Hello Gradi ! "

4.3 Procedures with parameters in R


In the R language, when calling a procedure with multiple parameters, it is important to supply
the arguments in the order corresponding to the parameters. Parameters are used the same way as in
the algorithm, and arguments must be separated by commas.

The version of the example 4.3 with one parameter can be written as follows :

91
Example 4.4.

# The procedure
greeting <- function ( firstName ) {
print ( paste ( " Hello " , firstName , " ! " ) )
}

When the procedure is called, we get the following result :


> # The procedure call
> yourFirstName <- readline ( " Input your first name : " )
Input your first name : Gradi
> greeting ( yourFirstName )
[1] " Hello Gradi ! "

Here is the script corresponding to the previous example where we wrote a program to display a
fragment of the multiplication table (example 2.4).

Example 4.5 (Multiplication table in R).

tableMulti <- function ( base , starting , ending ) {


print ( paste ( " Fragment of the multiplication table by " , base , " : " ) )
print ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " )

n <- starting
while ( n <= ending ) {
print ( paste (n , " x " , base , " = " , n * base ) )
n <- n + 1
}
}

After calling the procedure tableMulti(4, 1, 12), the result obtained is the following :

> tableMulti (4 , 1 , 12)


[1] " Fragment of the multiplication table by 4 : "
[1] " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
[1] " 1 x 4 = 4 "
[1] " 2 x 4 = 8 "
[1] " 3 x 4 = 12 "
[1] " 4 x 4 = 16 "
[1] " 5 x 4 = 20 "
[1] " 6 x 4 = 24 "
[1] " 7 x 4 = 28 "
[1] " 8 x 4 = 32 "
[1] " 9 x 4 = 36 "
[1] " 10 x 4 = 40 "
[1] " 11 x 4 = 44 "
[1] " 12 x 4 = 48 "

92
4.4 Functions in R
In R, functions are structured similarly to procedures, but they differ in their ability to return a
single value. This can be illustrated by the following example, allowing to calculate the sum of two
numbers :

Example 4.6.

sum <- function ( num_1 , num_2 ) {


sum_result <- num_1 + num_2
return ( sum_result )
}

4.5 Subroutine parameters in R


4.5.1 Passing parameters in R
In R, parameters are passed by value. This means that when an object is passed to a subroutine
as a parameter, a copy of the object’s value is created and passed to the subroutine. So any changes
made to the object inside the subroutine will not affect the original object outside the subroutine.

Example 4.7.

# The subroutine incrementer ()


incrementer <- function ( valeur ) {
value <- value + 1
}

Assigning a value to the variable x and calling the subroutine incrementer , and displaying the
variable produces the following result
> # The main program
> x <- 5
> incrementer ( x )
> print ( x )
[1] 5

However, there is an exception for objects of type data.frame and matrix which can be passed by
reference. This means that when a Dataframe or Matrix is passed to a function as a parameter, changes
made to the object inside the function will be reflected outside of it. This is due to the way these
objects are stored in memory in R.

4.5.2 Default Arguments in R


In the definition of a subroutine in R, it is possible (and often desirable) to define default values
for the parameters. This allows the subroutine to be called with only part of the expected arguments.

Take the example of the greeting subroutine. We can customize the greeting message as shown
below :

Example 4.8.

93
greeting <- function ( firstName , message = " Hello " ) {
print ( paste ( message , firstName , " ! " ) )
}

Using this subroutine can give the following :


> greeting ( " Paul " )
[1] " Hello Paul ! "
> greeting ( " Paul " , " Good Morning " )
[1] " Good Morning Paul ! "

It is important to note that parameters without default values must be placed before parameters with
default values in the subroutine definition in R. This is due to the way arguments are associated with
parameters when calling of the subroutine. By respecting this order, the first arguments provided du-
ring the call are associated with the corresponding parameters. Then parameters with default values
are associated with the remaining arguments, if provided. This approach ensures a consistent mapping
between arguments and parameters, making it easier to understand and use the subroutine.

Here is an example to illustrate the importance of the order of the parameters :


example <- function ( parameter1 , parameter2 = " default value " ,
parameter3 ) {
# Body of the subroutine
}

Subroutine calls with parameters


example ( parameter1 , parameter3 ) # Correct
example ( parameter1 , parameter2 , parameter3 ) # Correct

# Syntax error if the order of parameters is incorrect


example ( parameter2 , parameter1 , parameter3 ) # Error !

In this example, if we set the parameter parameter2 with a default value before parameter1, it will
cause a syntax error. By respecting the order of parameters without default value before parameters
with default values, the association between arguments and parameters is done correctly when calling
the subroutine. This also ensures consistency and predictability in the behavior of the subroutine.

4.5.3 Arguments with labels in R


In R language, it is generally necessary to provide the arguments in the same order as the cor-
responding parameters in the definition of a function. However, R also provides the ability to specify
arguments using their names, allowing them to be provided in any order.

Let’s take the example of the multiplication table that we mentioned earlier and adapt it to the
R language :

Example 4.9 (Multiplication table in R).

tableMulti <- function ( base = 4 , starting = 1 , ending = 12) {


print ( paste ( " Fragment of the multiplication table by " , base , " : " ) )
print ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " )

94
n <- starting
while ( n <= ending ) {
print ( paste (n , " x " , base , " = " , n * base ) )
n <- n + 1
}
}

After calling the procedure tableMulti(starting=3, ending=14, base=5), the result obtained is
the following :

> tableMulti ( ending =3 , starting =14 , base =5)


[1] " Fragment of the multiplication table by 5 : "
[1] " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
[1] " 3 x 5 = 15 "
[1] " 4 x 5 = 20 "
[1] " 5 x 5 = 25 "
[1] " 6 x 5 = 30 "
[1] " 7 x 5 = 35 "
[1] " 8 x 5 = 40 "
[1] " 9 x 5 = 45 "
[1] " 10 x 5 = 50 "
[1] " 11 x 5 = 55 "
[1] " 12 x 5 = 60 "
[1] " 13 x 5 = 65 "
[1] " 14 x 5 = 70 "

4.6 Scope of a variable in R


Here is the example of the variable with global scope :

Example 4.10 (Global variable in R).

# Declaration of a global variable


x <- 5

# The procedure with a local variable


add <- function () {
# Declaration of a local variable
y <- 10
print ( x + y ) # Access to the global variable and to the local
variable
}

> add () # Procedure call


[1] 15

Here is the example of the variable with local scope :

Example 4.11 (Global variable in Python).

95
# Declaration of a global variable
x <- 4

# The procedure with a local variable of the same name


affichage <- function () {
# Declaration of a local variable of the same name
x <- 12
print ( x ) # Access to the local variable
}

> display () # Calling the procedure


[1] 12
> print ( x ) # Access to the global variable
[1] 4

In R, it is not possible to directly modify a global variable inside a subroutine. However, there is an
alternative approach to achieve a similar effect using a return function.

Example 4.12 (Global variable in Python).

# Declaration of a global variable


x <- 4

# The procedure with a local variable of the same name


display <- function () {
x <<- 12 # Changing the global variable
print ( x ) # Displays the modified global variable
}

> display ()
[1] 12
> print ( x )
[1] 12

4.7 Recursive functions in R


R also allows the use of recursive functions. Here are some illustrative examples :

Example 4.13 (Factorial of a number in R).

facto <- function ( n ) {


if ( n == 0) {
return (1)
} else {
return ( n * facto ( n - 1) )
}
}

Example 4.14 (Euclidean algorithm in R).

96
gcd <- function (a , b ) {
if ( b == 0) {
return ( a )
} else {
return ( gcd (b , a %% b ) )
}
}

4.8 Anonymous functions in R


In R, anonymous functions are created using the keyword function, followed by the function
parameters and the function body in braces.
function ( arguments ) {
expression
}

Example 4.15 (Simple example of an anonymous function in R).

f <- function ( x ) {
return (2* x ^2+4)
}

The example program 4.15 can be simply written as follows :


> f <- function ( x ) 2* x ^2+4

This is what running the function gives :


> f (5)
[1] 25

Example 4.16 (Currying in R).

sommeC <- function ( x ) {


fonction <- function ( y ) {
fonction_interne <- function ( z ) {
return ( x + y + z )
}
return ( fonction_interne )
}
return ( fonction )
}

> sommeC (5) ( -3) (5.0)


[1] 7

This is what running the function gives :


> f (5)
[1] 25

97
5 Comparison of Functions and Procedures concepts in Python
and R
Below is a comparative table of the notions of procedure and function in Python and R :

Table 1 – Comparison of Functions and Procedures concepts in Python and R.

Notion Python R
Definition
def procedure_name ( procedureName <-
parameters ) : function ( parameters )
instructions {
instructions
}

Call
procedure_name ( arg1 , procedureName ( arg1 , arg2
arg2 ) )

Return value
return valeur return ( valeur )
Unlike Python, the use of the key-
word return is not necessary, be-
cause the return value is determi-
ned by the last evaluated expres-
sion. Procedures can have an en-
vironment that contains the func-
tion’s local variables.

Passing Parame- Arguments are usually passed by Parameters are usually passed by
ters reference. value.

Local variables
local_variable = value localVariable <- value

Global variables
global global_variable globalVariable <<- value

98
Table 2 – Comparison of Functions and Procedures concepts in Python and R (Continued).

Notion Python R
Anonymous func-
tion lambda arguments : function ( arguments ) {
expression expression
}

6 Conclusion
In conclusion, procedures and functions are essential elements of structured programming, allowing
to solve problems by dividing them into smaller and reusable tasks. Both Python and R provide data
scientists with the ability to program with procedures and functions in an easy way, making it easier
to organize and reuse code, improving project efficiency and maintenance.

In addition, Python and R offer the possibility of implementing functional programming, a pro-
gramming paradigm particularly well suited to Data Science applications. This paradigm offers po-
werful concepts for manipulating and analyzing data, as well as developing sophisticated algorithms.

In a later paper, we shall also discuss how to group these functions and/or procedures together to
form modules, providing an even more structured and modular approach to programming.

99
Références
Cormen, T., Leiserson, C., Rivest, R., and Stein, C. (2004). Introduction à l’Algorithmique. Cours et
exercices. Dunod, Paris, 2ème éd. edition.

Curry, H. B. (1980). Some Philosophical Aspects of Combinatory Logic. Studies in Logic and the
Foundations of Mathematics, 101 :85–101.

Darmangeat, C. (2008). Algorithme et Programmation pour les non-matheux. Cours complet avec
exercices, corrigés et citations philosophiques. Université Paris 7.

Dasgupta, S. (2014). It Began with Babbage : The Genesis of Computer Science. Oxford University
Press, Oxford.

Davies, T. M. (2016). The Book of R - A First Course in Programming and Statistics. William
Pollock, San Francisco.

Euclide (1994). Les Éléments, volume vol. 2. Livres V à IX. Notes et commentaires par Bernard
Vitrac. PUF, Paris.

Fernandez, M. (2009). Models of Computation : An Introduction to Computability Theory. Undergra-


duate Topics in Computer Science. Springer Science & Business Media.

Heim, I. and Kratzer, A. (1998). Semantics in Generative Grammar. Blackwell.

Kamingu, G. L. (2022). Python vs. R pour Data Scientists : Structures algorithmiques. Série Optimall
Python vs. R pour Data Scientists, 001(003).

Knuth, D. E. (1997a). The Art of Computer Programming : Fondamental Algorithms, volume 1.


Addison-Wesley Longman Publishing Co., Inc., USA, 3rd edition.

Knuth, D. E. (1997b). The Art of Computer Programming : Seminumerical Algorithms, volume 2.


Addison-Wesley Longman Publishing Co., Inc., USA, 3rd edition.

Mauchly, J. W. (1982). Preparation of problems for EDVAC-type machines. In Randell, B., editor,
The Origins of Digital Computers, pages 393–397. Springer.

Quine, W. V. O. (1967). On the building blocks of mathematical logic. In van Heijenoort, J., edi-
tor, A Source Book in Mathematical Logic, 1879–1931, pages 355–366. Harvard University Press,
Cambridge, MA. Translated by Stefan Bauer-Mengelberg.

Reynolds, J. C. (1972). Definitional Interpreters for Higher-Order Programming Languages. In Pro-


ceedings of the ACM Annual Conference, volume 2, pages 717–740.

Swinnen, G. (2012). Apprendre à programmer avec Python 3. Eyrolles, Paris.

Turing, A. M. (1945). Report by Dr. A.M. Turing on proposals for the development of an Automatic
Computing Engine (ACE) : Submitted to the Executive Committee of the NPL in February 1946
reprinted in Copeland, B. J., ed. (2005). Alan Turing’s Automatic Computing Engine. Oxford
University Press, Oxford.

Turner, D. (1997). Programming language, Currying, or Schonfinkeling ? Consulté le 3 mars 2022.

Wheeler, D. J. (1952). The use of sub-routines in programmes. In Proceedings of the 1952 ACM
national meeting, page 235, Pittsburgh. ACM.

100
Wilkes, M. V., Wheeler, D. J., and Gill, S. (1951). Preparation of Programs for an Electronic Digital
Computer. Addison-Wesley.

Wirth, N. (1976). Algorithms + Data Structures = Programs. Prentice-Hall, New Jersey.

101

You might also like