Python/Basic functions: Difference between revisions
Appearance
New page: A function is a way of tying a bunch of code together so that it can be used at a later time. To create a function, it must be defined with the def command. >>>def example(): prin... |
No edit summary |
||
| Line 1: | Line 1: | ||
A function is a way of tying a bunch of code together so that it can be used at a later time. To create a function, it must be defined with the def command. | A function is a way of tying a bunch of code together so that it can be used at a later time. To create a function, it must be defined with the def command. | ||
>>>def example(): | >>>def example(): | ||
print 'I am a seemingly self aware function!' | print 'I am a seemingly self aware function!' | ||
| Line 7: | Line 7: | ||
Merely typing the name of the function will show you such. | Merely typing the name of the function will show you such. | ||
>>>example | >>>example | ||
<function example at 0x01680730> | <function example at 0x01680730> | ||
To actually call the function, you have to include parenthesis afterwards. | To actually call the function, you have to include parenthesis afterwards. | ||
>>> example() | >>> example() | ||
I am a seemingly self aware function! | I am a seemingly self aware function! | ||
The reason for the parenthesis is so that you can pass arguments to functions that require them. | The reason for the parenthesis is so that you can pass arguments to functions that require them. | ||
>>> def square(x): | >>> def square(x): | ||
print str(x**2) | |||
>>> square(4) | |||
>>> square(4) | 16 | ||
16 | |||
If we wanted to use the value that the square function gives us, we would rewrite it make use of the return command. | If we wanted to use the value that the square function gives us, we would rewrite it make use of the return command. | ||
>>> def square(x): | >>> def square(x): | ||
return (x**2) | return (x**2) | ||
Which can then be used to do such fun things as: | Which can then be used to do such fun things as: | ||
>>> print square(3) | >>> print square(3) | ||
9 | 9 | ||
or | or | ||
>>>print square(square(3)) | >>>print square(square(3)) | ||
81 | 81 | ||
or even in statements such as: | or even in statements such as: | ||
>>>10 + square(2) | >>>10 + square(2) | ||
14 | 14 | ||
and | and | ||
>>> y = square(10) | >>> y = square(10) | ||
>>> y | >>> y | ||
100 | 100 | ||
Revision as of 13:00, 1 August 2007
A function is a way of tying a bunch of code together so that it can be used at a later time. To create a function, it must be defined with the def command.
>>>def example():
print 'I am a seemingly self aware function!'
This assigns the name 'example' in the same way that the statement 'x = 1' assigns the name x to the value 1. Merely typing the name of the function will show you such.
>>>example <function example at 0x01680730>
To actually call the function, you have to include parenthesis afterwards.
>>> example() I am a seemingly self aware function!
The reason for the parenthesis is so that you can pass arguments to functions that require them.
>>> def square(x):
print str(x**2)
>>> square(4)
16
If we wanted to use the value that the square function gives us, we would rewrite it make use of the return command.
>>> def square(x):
return (x**2)
Which can then be used to do such fun things as:
>>> print square(3) 9
or
>>>print square(square(3)) 81
or even in statements such as:
>>>10 + square(2) 14
and
>>> y = square(10) >>> y 100