Monday, April 26, 2021

The way of function interacts with its arguments

 Consider the below example.

Example - 1







The output is given below.





Here, changing the parameter's value doesn't propagate outside the function.

This also means that a function receives the argument's value, not the argument itself. This is true for scalars.


Let's apply this theory for lists.

Example - 2







The output is,





Example - 3







The output is,




Explanation:-

  1. If the argument is a list, then changing the value of the corresponding parameter doesn't affect the list (But variables containing lists are stored in a different way than scalars)
  2. But if we change a list identified by the parameter (not the parameter), the list will reflect the change.

Functions & Scopes - the "global" keyword

There's a special Python method which can extend a variable's scope in a way which includes the functions' bodies even if we want not only to read the values, but also to modify them.

This effect is caused by a keyword named "global".

Using this keyword inside a function with the name or names separated with commas of a variable(s), forces Python to refrain from creating a new variable inside the function - the one accessible from outside will be used instead.

In other words, this name becomes global. It has a global scope and it doesn't matter whether it's the subject of read or assign.


Look at the example given below.



Here the output is,


Saturday, July 4, 2020

Functions & Scopes


The scope of a name (e.g., a variable name) is the part of a code where the name is properly recognizable.

For example, the scope of a function's parameter is the function itself. The parameter is inaccessible outside the function.

Let’s consider the below snippet.








Here, we get the following error message.





So let’s consider the below snippet.










Output is







A variable existing outside a function has a scope inside the functions’ bodies, excluding those of them which define a variable of the same name.


It also means that the scope of a variable existing outside a function is supported only when getting its value (reading). Assigning a value forces the creation of the function's own variable.

Lists & Functions


A list can also be sent to a function as an argument. So if we pass a list to a function, the function has to handle it like a list. 
Consider the below snippet.













Output is 12.


But if we invoke to the function in below way:






Python will response in below way.






This is caused by the fact that a single integer value mustn't be iterated through by the “for” loop.


A list can also be a result of a function. It mean, any entity recognizable by Python can be a function result.

Consider the below snippet.











Output is



"None" keyword


Its data doesn't represent any reasonable value - actually, it's not a value at all; hence, it mustn't take part in any expressions.

“None” is a keyword.

Let’s consider the below snippet & output.








This cause a runtime error, described by the above message.

There are only two kinds of circumstances when “None” can be safely used:
  • when you assign it to a variable (or return it as a function's result)
  • when you compare it with a variable to diagnose its internal state.
Like below.







If a function doesn't return a certain value using a return expression clause, it is assumed that it implicitly returns “None”.

Sometimes it may be the symptom of a subtle mistake inside the function.








Output is

The “return” instruction


This is a keyword in Python. This instruction has two different variants. Let’s consider them separately.

“return” without an expression

When it used inside a function, it causes the immediate termination of the function’s execution, and an instant return (hence the name) to the point of invocation.

If a function is not intended to produce a result, using the “return” instruction is not obligatory. It will be executed implicitly at the end of the function.

Let’s consider the below example.













Output will be







And the next example is












Output will be








“return” with an expression

The second “return” variant is extended with an expression.

function () :
   return expression

So, here consider the below example.










Result is





Sometimes that function not return the value and ignore it.











Output is


Parameterized Functions


It happens at times that a particular parameter's values are in use more often than others. Such arguments may have their default (predefined) values taken into consideration when their corresponding arguments have been omitted.

Let’s consider the below example.









Output will be,







Both parameters have their default values now, look at the code below.







Output will be