Help the teapot understand an example from functional programming

Dear community, I have to turn to you for help, please do not judge strictly, I am studying Python. At the beginning of the section on OOP, there is a small chapter about functional programming. As always, all the code that is served is typed in PyCharm. That's actually what interested me:

enter a description of the image here

With the first function, everything is clear to me. But when trying to output a function from the second example:

def increment(a):
    return a + 1

increment(a)

I get an error message that the variable and not defined. In this regard, the first question: where to define a variable if, by definition, in functional programming, the function does not rely on data outside of it and does not change it? And in this example, I don't see that the data is defined inside the function.

The second question arose after studying and testing the same function:

Why this code:

def increment(a):
    return a + 1
    print(a)

increment(1)

It doesn't output anything(not even errors), but we have a print statement...

A code:

def increment(a):
    return a + 1

print(increment(1))

Outputs the value (which is generally true).

Author: VartaFather, 2020-08-24

4 answers

def increment(a):
    return a + 1

increment(a)

You call a method in a global environment in which the variable "a" is not defined and you try to pass it, define it before calling the method

def increment(a):
    return a + 1
    print(a)

increment(1)

Return causes the method to exit, so the print function is not called

def increment(a):
    return a + 1

print(increment(1))

This is true because you are returning the result of a function from a method to the Print

UPD for the first option, this is how you can define the variable

a = 7
def increment(a):
    return a + 1
increment(a)

Or like this

def increment(a):
    return a + 1
a = 7
increment(a)

The main thing is that it is in the same area visibility and before calling the function

 6
Author: Дима Савичев, 2020-08-24 19:10:27

Where to define the variable

It is a variable, in the sense that imperative languages put in this word – preferably nowhere.

In functional programming terms, a variable is a name that can be associated with an arbitrary value, but cannot be reassigned within a single calculation. I.e., it means a variable in the mathematical sense.

t = 3x

This is a typical action in mathematics, a

x = x + 1

Nonsense, since

x - x = 1
0 = 1

And functional programming is built on mathematical laws.

In python, there is no explicit mechanism that prohibits the redefinition of variables, this is on the conscience of the programmer. If it does not reassign variables and does not change their contents in any other way, then this does not contradict the principles of functional programming, and such variables are necessary and even necessary.

By definition, in the functional in programming, the function does not rely on data outside of it and does not change it

A pure function does not rely on external mutable data and does not change it itself, it does. But no one prevents it from accessing external immutable data, for example:

def circumference(r):
    return 2 * math.pi * r

This function is pure, again with a caveat, if someone wants to reassign the value of math.pi, the language will not prevent this in any way, it implies that the user will have enough sanity not to do this.

And even in the following example, both f and g functions are pure

def f(x):
    def g(y):
        return 2 * x + 3 * y

    return g(1) * g(2)

Although g relies on an external parameter x, it is immutable within the calculation of the value f(x), and g for different values x are different functions, and not the same function giving different results.

 4
Author: extrn, 2020-08-25 06:59:56

Here's how to define a variable:

def increment(a):
    return a + 1
a = 1
print(f"До использования функции а={a}")
a = increment(a)
print(f"После использования функции а={a}")

And here you have a mistake:

def increment(a):
    return a + 1
    print(a)

You need to put print(a) over return - because in fact this command completes the execution of the function and the print function is no longer executed (due to indents that replace formatting with parentheses in other languages):

def increment(a):
    result = a + 1            
    print(result)
    return result

UPD. In your listing from the book in the question, by the way, in the first case, the variable а=0 is declared before the function, and it is passed to the function not through arguments, but through global, and in the second case, as an argument.

 2
Author: Serg Bocharov, 2020-08-24 19:19:32

On the second question:

You don't need to define a variable anywhere.

If the language has support for tail recursion (python does not have it in the box), then this dynamic world will work:

def world_iteration(a,b):
  will_a = increment(a)
  will_b = increment(b)
  world_iteration(will_a,will_b)

world_iteration(0,0)
 2
Author: Sergei Kirjanov, 2020-08-24 21:26:59