Python/Tutorial/Functions/Introduction
Contents
Introduction
A function, or more technically correct, a callable is an object within Python that does something interesting when called. As you write your Python code, 99% of the time you will be creating functions and other types of callables.
Calling a function means passing control of the program to the function until the function completes. The function encapsulates or contains a bit of programming logic (code) that hopefully does what you expect.
Function Calls are Expressions
Function calls are expressions, not statements. That means that anywhere you can use an expression, you can also call a function.
Function Arguments are Expressions
The arguments you specify to the function are also expressions. Note that the arguments are always evaluated before the function is ever called. You are not passing names or expressions in as arguments, you are passing the values that the expressions reduce to.
Functions can Return Anything
Functions can return anything, from ints to longs, to None, to bools or even lists, tuples or dicts.
Code Does Not Continue Until Function Completes
When you call a function, your program is on hold until that call completes.
Scoping
The variables used inside of a function call do not affect variables outside of the function call (unless they happen to refer to the same values one way or another.) The code inside the function is thus isolated from the calling code.
The Stack
You may have heard of the stack. In fact, every time you've seen an expression, a stack trace was printed out to show you where the exception occurred.
The stack (as opposed to a stack) is the function call stack. It is a stack, which we've talked about before concerning lists. When a function call is made, Python pushes the execution environment (known as the frame) for the function body execution on the stack. When it returns, Python pops it off the stack and throws it away. If an exception occurs, Python has available to you the entire calling stack, so you can see who called what and in what order.
In relation to the stack, there is on, yet confusing thing I want to introduce. First, not every program uses a stack. It is possible to build Python such that instead of pushing frames onto a stack, Python could simply remember which frame is connected to which frame. In essence, it can have many stacks at the same time. The 'greenlets' module (not part of the standard distribution) allows you to do some fairly magical things by manipulating the stack in this way. We often call this style of programming "micro threads". We won't cover this as part of the tutorial, although I want you to be aware of it.
Functions are First-Order Objects
Many programming languages treat functions as something special. Python does not. They are objects like any other, with the added attribute of being callable.
This has repercussions. You can send functions---not the result of a call to a function, but the function itself---as parameters to a function call. Or you may receive a function as the returned value.
We'll cover why this is useful and how to take advantage of it later. For now just know that functions are not magic. You can talk about them the same way you talk about strings and ints.