11.2. Calling Functions

11.2.1. Function Operator

Functions are called using the same pair of parentheses that you are used to. In fact, some consider ( ( ) ) to be a two-character operator, the function operator. As you are probably aware, any input parameters or arguments must be placed between these calling parentheses. Parentheses are also used as part of function declarations to define those arguments. Although we have yet to formally study classes and object-oriented programming, you will discover that the function operator is also used in Python for class instantiation.

11.2.2. Keyword Arguments

The concept of keyword arguments applies only to function invocation. The idea here is for the caller to identify the arguments by parameter name in a function call. This specification allows for arguments to be missing or out-of-order because the interpreter is able to use the provided keywords to match values to parameters.

For a simple example, imagine a function foo() which has the following pseudocode definition:

							def foo(x):
    foo_suite   # presumably does so processing with 'x'

Standard calls to foo(): foo(42) foo('bar') foo(y)

Keyword calls to foo(): foo(x=42) foo(x='bar') foo(x=y)

For a more realistic example, let us assume you have a function called net_conn() and you know that it takes two parameters, say, host and port:

							def net_conn(host, port):
    net_conn_suite
						

Naturally, we can call the function giving the proper arguments in the correct positional order which they were declared:

net_conn('kappa', 8080)

The host parameter gets the string 'kappa' and port gets 8080. Keyword arguments allow out-of-order parameters, but you must provide the name of the parameter as a “keyword” to have your arguments match up to their corresponding argument names, as in the following:

net_conn(port=8080, host='chino')

Keyword arguments may also be used when arguments are allowed to be “missing.” These are related to functions which have default arguments, which we will introduce in the next section.

11.2.3. Default Arguments

Default arguments are those which are declared with default values. Parameters which are not passed on a function call are thus allowed and are assigned the default value. We will cover default arguments more formally in Section 11.5.2

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset