-2
Okay, confusion is with unified syntax. A lot of people don’t really understand the syntax of the language, whatever it is. It is important to understand everything that is happening and to know what each character does in the code, what is happening there, to understand the concepts, and all interconnections of each existing mechanism in the language. I say this in my lectures and even put here, but people do not realize how important it is to program well:
You know What is a variable?
You know What is a literal?
So when funcao()
is called it returns the value contained in the identifier called f
. It turns out that this identified has as value a function, described just above. So it’s like a variable (technically it’s a constant, even though Python doesn’t have this concept for other forms). When the return is made you can use this value somewhere. One of the things you can do is store it in a variable. But you don’t have to do that, you can use it right there in the expression you’re using, you use it in a way inline.
So let’s think of a slightly different code:
def funcao():
return 1
variavel = funcao() + 2
print variavel
Can you tell what’s going on there? After the return of the value it’s like the code is like this:
variavel = 1 + 2
print variavel
It’s pure mathematics, we replaced something we previously didn’t know about the value, so it was a variable or constant, with a known value, and as we do that we get a result. In the case funcao()
is what we didn’t know the value and which was replaced by something known after the function was called by returning the value.
You can understand that?
def f(var):
print(var)
def funcao():
return f
variavel = funcao()
variavel('OI')
What is being stored in variavel
? It’s a function called f
, right? So now variable `has as its value a function that I can call it.
Then making the replacement of the function by its return would be so:
def f(var):
print(var)
variavel = f
variavel('OI')
And to call a function always use the parentheses right after the identifier that names the function, can pass arguments or not, depending on the function. In this case the function I have is stored in the variable with the identifier variavel
and so soon after called this function with this identifier passing an argument within the parentheses.
Now, because I need to save something in a variable that I just want the result. If I didn’t know, now reading the link above you should know that a variable is a name for a storage location, and in your code you do not need to store anything,, just take the value and use.
Then we return to the initial example of the answer where you receive a value returned by the function and already use immediately. Instead of using the addition operator +
as I used there, is now using function call operator ()
, or as was used now passing argument ('OI')
, so the literal 'OI'
It’s like I’m the 2
of my first example.
Note that there is nothing special about the concept. It just had an intersection of different mechanisms. Probably already knew how to call a function and use in an expression. Probably already knew uses ruma function anonymous, but for some reason thought that could not use the two together equal does with a numerical value or otherwise. It is a linear question, if thinking well it would not even make sense to be different, if it works one way why would it prevent it from working another? There may be some reason, but in this case there is.
So replacing the function with its return in your code would be:
def f(var):
print(var)
variavel = f('OI')
I put in the Github for future reference.
in that case nor does it make sense to have the variable variavel
because it stores a null value since it even has a return
.
I wanted to know where is being returned the function f and why put the second parenthesis with value ('hi') because if I give a print on the variable the value will be None!
– user217941
If it were
x = funcao()
, what would be the value ofx
?– Woss
Unintentionally, it almost manages to implement a more sophisticated language device where many people have difficulty understanding how it works.
– Augusto Vasques
The value of x would be None
– user217941