Calling Functions

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.

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):
	print host
	print port

>>> net_conn("localhost",8080)

You may also like...