Python defines functions that are not executed until explicitly called. Functions must be defined before they are invoked. Python reads code sequentially, so functions must be defined before their invocation. Positional arguments are values passed to corresponding parameters in a function. Keyword arguments associate values with parameters based on the parameter name rather than position. None is a special value that does not represent any actual value and cannot be used in expressions but can be assigned to variables or compared with variables.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views
Functions
Python defines functions that are not executed until explicitly called. Functions must be defined before they are invoked. Python reads code sequentially, so functions must be defined before their invocation. Positional arguments are values passed to corresponding parameters in a function. Keyword arguments associate values with parameters based on the parameter name rather than position. None is a special value that does not represent any actual value and cannot be used in expressions but can be assigned to variables or compared with variables.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Python reads the function’s definitions and remembers them, but
won’t launch any of them without your permission. The function is
launched or invoked when an explicit call is made.
You mustn’t invoke a function which is not known at the
moment of invocation. Remember – Python reads your code from top to bottom. It’s not going to look ahead in order to find a function you forgot to put in the right place (“right” means “before invocation”) You mustn’t have a function and a variable of the same name.
Don’t forget:
parameters live inside functions (this is their natural
environment) arguments exist outside functions, and are carriers of values passed to corresponding parameters. A technique which assigns the i (first, second, and so on) th
argument to the i (first, second, and so on) function
th
parameter is called positional parameter passing,while
arguments passed in this way are named positional arguments. You’ve used it already, but Python can offer a lot more. We’re going to tell you about it now. Note: positional parameter passing is intuitively used by people in many social occasions. For example, it is generally accepted that when we introduce ourselves we mention our first name(s) before our last name.
Python offers another convention for passing arguments, where the
meaning of the argument is dictated by its name, not by its position – it’s called keyword argument passing. Take a look at the snippet → 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. Of course, you mustn’t use a non-existent parameter name. If you try to pass more than one value to one argument, all you’ll get is a runtime error. TypeError: sum() got multiple values for argument 'a'
Return – Python keyword
When 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.
Note: 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.
Anyway, you can use it to terminate a function’s activities on
demand, before the control reaches the function’s last line. Let us introduce you to a very curious value (to be honest, a none value) →
data of this value doesn’t represent any reasonable value –
actually, it’s not a value at all;
hence, it mustn’t take part in any expressions.
For example, a snippet like this:
print(None + 2)
will cause a runtime error, described by the following diagnostic
message:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Note: None is a keyword.
There 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.
a variable existing outside a function has a scope inside the
functions’ bodies.
This rule has a very important exception. Let’s try to find it.
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.