Python. Recommendations for the correct structuring of programs using functions

Recommendations for the correct structuring of programs using functions. Implicit function call. Examples


Search other websites:

1. Recommendations for the development (design) of functions and modules in programs

When writing large programs, it becomes necessary to structure them into functions. To correctly build a program using functions, you need to solve the following main problems:

  • the problem of connectivity, which consists in the optimal division of the problem into functions;
  • the problem of interaction. In this case, you need to decide how the functions should interact with each other;
  • ensuring the optimal size of the function.

To overcome the above problems, when designing programs using functions, the following recommendations (rules) should be observed.

To solve the problem of interaction, you need:

  • to ensure maximum independence of the function from the main program. To do this, it is desirable to pass the value of the function using arguments, minimizing the use of global variables. You need to return a value from a function using the return statement;
  • use global variables to interact with a function only as a last resort. If you massively use global variables in program functions, it can lead to subtle consistency errors;
  • ensure correct use of arguments. If, when passing mutable arguments from the calling program to the function, they are not expected to change, then these arguments do not need to be changed in the function. As you know, functions can modify some mutable objects received as arguments. Changing the arguments in the body of the function increases the relation between the function and the program and makes it less independent and more specific;
  • avoid directly modifying variables in another module. If you change the variables of another module in one module, then a close connection is established between these modules. The modules are difficult to understand and unsuitable for reuse. It is necessary to ensure the independence of the program modules as much as possible. The direct operation of assigning a variable in another module must be replaced by a call to a specially designed access function from the same module.

Solving the connectivity problem requires each function in the program to have a unique purpose. Each function must perform one specific action (operation) in the program. The programmer must be able to correctly (optimally) highlight the parts of the program to represent them in the form of functions.

From a size issue perspective, a feature-set program needs to be designed so that each feature in the program is relatively small in size. If a function becomes too cumbersome and difficult to perceive, then you need to split this function into parts or change the entire process of designing the task.


2. The scheme of organizing the interaction of a function with other parts of the program

Figure 1 shows a diagram of how the function interacts with other parts of the program.

Python. Interaction of the function with external parts of the program

Figure 1. Interaction of the function with external parts of the program

As you can see from the figure, a function can receive data and return results in various ways. However, the most recommended is to take input values as arguments.

Regarding the return of results, it is advisable to implement them:

  • using the operator return;
  • using mutable arguments, provided the calling program assumes it.


3. Functions as objects. Operations on references to a function object

In Python, functions are objects. The function name is a reference to the object that implements this function. Based on this, various operations can be performed on functions:

  • assign a function name to another reference;
  • call the work of the function by reference to it;
  • use a reference to a function object as a parameter in another function;
  • store references to objects of functions as data structures.


4. Implicit function call. Concept. Example

The Python programming language allows you to create a reference to a function. Using a reference to a function, you can call that function for execution. In Python, several references (calls) can be implemented for the same function. To get a reference to some function, you need to assign the function name to this reference. In general, the assignment code looks like this

# The function to which the reference will be declared
def Fn(parameters):
    # Function code

# Get the reference to the function
refFn = Fn

# Call a function by reference, parameters must match


  • Fn – function name, to which the reference is being declared;
  • refFn – reference to the function Fn();
  • parameters – a list of parameters that the function receives.

Calling a function by reference to it is called an implicit function call. If the function is called by reference, then the number and types of parameters must be the same when calling the function. Otherwise, an error will be generated.

Example. The example demonstrates assigning the name of the Add() function to another reference to the fn() function. Then, the fn() reference calls the Add() function to calculate the sum of the two numbers.

# Reference to a function. Calling a function by reference

# Function that returns the sum of two numbers
def Add(x, y):
    return x+y

# Assign function name to another name
fn = Add # fn - reference to Add() function

# Invoke function Add() by reference fn
z = fn(3, 8)
print("z = ", z) # print the result


5. An implicit function call with a different number of parameters. Finding the maximum value between numbers. Example

The example demonstrates calling one of the 3 functions by reference:

  • Max2() – determines the maximum value between two numbers;
  • Max3() – determines the maximum value between three numbers;
  • Max4() – determines the maximum value between four numbers;


# Max2() - returns the maximum between two numbers
def Max2(a, b):
    if a>b:
        return a
    return b

# Max3() - maximum between three numbers
def Max3(a, b, c):
    max = a
    if max<b: max=b
    if max<c: max=c
    return max

# Max4() - maximum between four numbers
def Max4(a, b, c, d):
    max = a
    if max<b: max=b
    if max<c: max=c
    if max<d: max=d
    return max

# Demonstrate implicit function call Max2(), Max3(), Max4().
fn = Max2 # fn reference points to Max2() function
maximum = fn(2.85, 7.6)
print("maximum = ", maximum)

fn = Max3 # fn reference points to Max3() function
maximum = fn(8.2, 3.44, 11.2)
print("maximum = ", maximum)

fn = Max4 # fn reference points to Max4() function
maximum = fn(1.6, 2.8, 0.3, 2.9)
print("maximum = ", maximum)

The result of the program

maximum = 7.6
maximum = 11.2
maximum = 2.9


Related topics