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


Mixing Positional & Keyword Arguments


We can mix both fashions if we want - there is only one unbreakable rule: we have to put positional arguments before keyword arguments.

Let’s consider below snippet.






We can get the output as





This is a pure example of positional argument passing. Also we can replace these arguments like below way.





So the output will be





Let’s try to mix both styles now.






Output will be





Let's analyze it:
  • The argument (3) for the “a” parameter is passed using the positional way.
  • The arguments for c” and “b” are specified as keyword ones.

But if we try to pass more than one value to one argument, all we'll get is a runtime error. Look at the snippet below.







This will get the following error.






Look at the snippet below. This code is fully correct, but it doesn’t make much sense.






Output is





Everything is correct. But leaving in just one keyword argument looks a bit weird.

Thursday, July 2, 2020

Keyword Argument Passing


Here the meaning of the argument is dictated by its name, not by its position.

Look at the below snippet.








Output is






The concept is clear - the values passed to the parameters are preceded by the target parameters' names, followed by the “=” sign.

The position doesn't matter here - each argument's value knows its destination on the basis of the name used.

Positional Parameter Passing


A technique which assigns the ith (first, second, and so on) argument to the ith (first, second, and so on) function parameter is called positional parameter passing, while arguments passed in this way are named positional arguments.

Let’s consider the below snippet.








Output is




Positional parameter passing is intuitively used by people in many social occasions. For example, it may be generally accepted that when we introduce ourselves we mention our first name(s) before our last name, e.g., "My name's John Doe".

Let's implement that social custom in Python.









Output is



Parameterized Functions


A parameter is actually a variable, but there are two important factors that make parameters different & special.

Parameters exist only inside functions in which they have been defined, and the only place where the parameter can be defined is a space between a pair of parentheses in the “def” statement.

Assigning a value to the parameter is done at the time of the function’s invocation, by specifying the corresponding argument.

We should remember this carefully. Specifying one or more parameters in a function’s definition is also a requirement, and we have to fulfill it during invocation. We must provide as many arguments as there are defined parameters.

Failure to do so will cause an error.

Let’s consider this example.








This code will produce the following output.





The value of the argument used during invocation (1) has been passed into the function, setting the initial value of the parameter named “number”.

Also it’s legal and possible to have a variable named the same as a function’s parameter. Let’s see the below snippet.










Output is






A situation like this activates a mechanism called shadowing.

Parameter “x” shadows any variable of the same name, but... only inside the function defining the parameter.

The parameter named “number” is a completely different entity from the variable named “number”.

A function can have as many parameters as we want, but the more parameters we have, the harder it is to memorize their roles and purposes.

Let's modify the function - it has two parameters now.










Output is


Functions


In Python, functions come from at least three ways.

1.      From preinstalled modules in Python.
2.      From built-in functions in Python.
3.      From directly in code.

Given below the format of a function.

def functionName():
     functionBody


·         It always starts with the keyword “def” (for define)
·         Next after “def” goes the name of the function (the rules for naming functions are exactly the same as for naming variables)
·         After the function name, there's a place for a pair of parentheses (they contain nothing here, but that will change soon)
·         The line has to be ended with a colon ( ; )
·         The line directly after “def” begins the function body - a couple (at least one) of necessarily nested instructions, which will be executed every time the function is invoked; note: the function ends where the nesting ends, so we have to be careful.


Consider the below example.









Output is




When the moment of the invocation of the function, there are two important factors to consider.

Firstly,

You mustn’t invoke a function which is not known at the moment of invocation.

Python reads your code from top to bottom. It’s not going to look ahead in order to find a function we forgot to put in the right place (right means “before invocation”).

Consider the below example.










Here we get the error message like this.



Secondly,

You mustn’t have a function and a variable of the same name.

Consider the below example. This is the erroneous snippet.







Assigning a value to the name message causes Python to forget its previous role. The function named “message” becomes unavailable.

We’re free to mix the code with functions. But we’re not obliged to put all the functions at the top of the source file.

Look at the snippet below.











This is also completely correct and works as intended.