[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
BeginnersGuideToPython_Functions
(remember that proper indentation of code blocks is important in Python!)
You can think of this as calling the built-in function def and passing it a function description. Note that although we have defined a function in the current namespace, the function doesn't really do very much. It does, however, do two interesting things. First, remember that all functions return a value. This function then returns None even though we didn't actually say "return None". Second, Python blocks must contain at least one statement - in our case we've simply defined a string. This fulfills the requirement that our function body block have a statement. It also allows us to quickly define function stubs which we can go back and fill out later. Furthermore, when a string is the first statement in a function body, Python makes that string the docstring of the function! After you have defined your function, you can refer to this docstring:
Built-in documentation of your code! How cool is that?!
This statement defines a function object named MyAdd that accepts two arguments, x and y, and returns the result of performing an addition operation on them. For example:
In this case, sum will clearly equal 3. But what if we did this:
What do you think the value will be? That's right, "foobar". Since Python variables are not defined as a specific type, the same function will work for any type for which the addition operator is defined. In the case of strings, "+" becomes the concatenation operator. In most other languages, you would have to define an entirely separate function to add strings than if you wanted to add numbers. And in some languages, you'd have to define separate functions for adding integers versus floating point numbers.
Be careful with this newfound power, though, it can bite you if you aren't careful:
In actuality, the function is only returning a single object, a tuple with five elements. Using the magic of Python, however, you can effectively return as many values as you want into separate variables
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
BeginnersGuideToPython_Functions
Beginner's Guide To Python, Functions
Function Basics
A function in Python is like a function in any other programming language. You define a block of code that does a specific thing so you can refer to it many times rather than writing the same block of code many times individually. Generically speaking, when a function is defined, the programmer specifies the name of the function, the arguments it accepts, the code it performs, and the value it returns (if any). In Python, functions always return a value. If you don't explicitly return a value, Python assumes the return value is None.The def Statement
In compiled languages, a function is defined in a source code file and the compiler converts it into machine instructions. Since Python is interpreted rather than compiled, you define a function by using the def statement. def is actually an executable statement that creates a function object. Here is an example:
>>> def MyFirstFunction():
"My First Function"
(remember that proper indentation of code blocks is important in Python!)
You can think of this as calling the built-in function def and passing it a function description. Note that although we have defined a function in the current namespace, the function doesn't really do very much. It does, however, do two interesting things. First, remember that all functions return a value. This function then returns None even though we didn't actually say "return None". Second, Python blocks must contain at least one statement - in our case we've simply defined a string. This fulfills the requirement that our function body block have a statement. It also allows us to quickly define function stubs which we can go back and fill out later. Furthermore, when a string is the first statement in a function body, Python makes that string the docstring of the function! After you have defined your function, you can refer to this docstring:
>>> def MyFirstFunction(): "My First Function" >>> print MyFirstFunction.__doc__ My First Function
Built-in documentation of your code! How cool is that?!
Passing Arguments to Functions
One of the most common uses of functions is to implement specific functionality that you want to use many times. It is helpful then to be able to pass some values to the block of code a function represents so that it can manipulate them and/or return some result(s). For example:
>>> def MyAdd(x, y):
"Add two values and return the sum."
return x + y
This statement defines a function object named MyAdd that accepts two arguments, x and y, and returns the result of performing an addition operation on them. For example:
>>> MyAdd(1,2) 3
In this case, sum will clearly equal 3. But what if we did this:
>>> MyAdd("foo","bar")
What do you think the value will be? That's right, "foobar". Since Python variables are not defined as a specific type, the same function will work for any type for which the addition operator is defined. In the case of strings, "+" becomes the concatenation operator. In most other languages, you would have to define an entirely separate function to add strings than if you wanted to add numbers. And in some languages, you'd have to define separate functions for adding integers versus floating point numbers.
Be careful with this newfound power, though, it can bite you if you aren't careful:
>>> MyAdd("foo",1)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in ?
MyAdd("foo",1)
File "<pyshell#11>", line 2, in MyAdd
return x + y
TypeError: cannot concatenate 'str' and 'int' objects
Returning Values from Functions
As we've already seen, functions will return None by default if you don't specify a return value or simply use the "return" statement by itself, or they will return the value you specify after the return statement. In many languages, functions are limited to a single return value. But in Python, you can do this:>>> def ReturnStuff(): return 1,2,3,4,5 >>> ReturnStuff() (1, 2, 3, 4, 5)
In actuality, the function is only returning a single object, a tuple with five elements. Using the magic of Python, however, you can effectively return as many values as you want into separate variables
>>> a,b,c,d,e=ReturnStuff() >>> a 1 >>> b 2 >>> c 3 >>> d 4 >>> e 5
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
