Python/Tutorial/Functions/Calling a Function

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Recall from our introduction that calling a function requires that you specify which function you want to call and what arguments you want to send.

As we've seen before, the syntax is simply:

function_name(args, ...)

Here, the parentheses are neither the parentheses that change the order of operations, nor are they the parentheses that specify tuples. These parentheses are used to specify which parameters you are sending to the function.

Note that the function name need not be a name---it can be an expression, as long as the expression evaluates to a callable!

When the function completes, it always returns a value, even if the value returned is None. The is the value that the function call expression evaluates to.

Specifying No Arguments

Many functions do not take any arguments.

>>> "asdf".upper()
'ASDF'

When you have no arguments, simply use '()'.

Specifying One Argument

Many functions take a single argument.

>>> len("asdf")
4

You may, if you feel so inclined, put a comma at the end of the argument list. Python will not complain.

>>> len("asdf",)
4

Specifying Multiple Arguments

You can specify more than one argument if the function accepts it.

>>> int('ff', 16)
255

Again, a trailing comma is allowed.

>>> int('ff', 16,)
255

Optional Arguments

Some functions have optional arguments. If you don't specify them, it should guess a good default.

>>> int('100') # base defaults to 10
100
>>> int('100', 10)
100

Keyword Arguments

Some functions have multiple parameters, and you may want to be specific about which argument goes to which parameter. You can do so by using the following syntax:

>>> int('ee', base=16)
238

As you can see from above, you can combine non-keyword arguments with keyword arguments, provided that all the keyword arguments follow all the non-keyword arguments.

Note that the order of the keyword arguments doesn't matter, as long as they follow all the non-keyword args.

>>> int(base=16, x='ee')
238

Pre-building Non-Keyword Args

You can use a tuple or list as the pre-built args. For instance:

>>> args = ['dd', 16]
>>> int(*args)
221

Note that you can specify non-keyword args before the pre-built args:

>>> args = [16]
>>> int('10', *args)
16

Pre-Building Keyword Args

Keyword args must be stored in a dict.

>>> kwargs = dict(x='ff', base=16)
>>> int(**kwargs)
255

You must specify the pre-built args after the non-keyword args and the keyword args. (I don't have an example, yet, of a function that is complicated enough that it requires this.)

What Functions Can Do To Your Args

In some programming languages, the function can modify the args you used as you passed them in. For instance, if you called a function as so:

foo(b)

it may assign b to a new value.

Python doesn't allow this.

However, in one special case a function may modify an argument's value. If you pass in a mutable object (such as a set, list or dict), the function may add, remove, or replace items within it. This does not mean the function can change your variable to point to a different object.

Investigating a Function

If you want some help understanding what a function's parameters are, you should read it's doc string, You can do so by simply printing out the __doc__ attribute:

>>> print int.__doc__
int(x[, base]) -> integer

Convert a string or number to an integer, if possible.  A floating point
argument will be truncated towards zero (this does not include a string
representation of a floating point number!)  When converting a string, use
the optional base.  It is an error to supply a base when converting a
non-string.  If base is zero, the proper base is guessed based on the
string content.  If the argument is outside the integer range a
long object will be returned instead.

You can also use the built-in help() function.

>>> help(int)
... lots of information (press q to quit)...

Function Signature Short Hand

You may have noticed a semi-standard short hand that functions are documented with. Let me explain what it means.

int(x[, base]) -> integer

Here, the arrow '->' means "This is what it will return".

Inside the argument list, you should see a list of parameter names. If they are optional, they will be surrounded by square braces '[' and ']'.