-
Notifications
You must be signed in to change notification settings - Fork 0
Functions
Theo Chupp edited this page Jul 21, 2017
·
3 revisions
Functions are similar to variables in that you can assign a name to a piece of code
When invoked, they are evaluated
They can have zero to many inputs, and an optional output
There are two important concepts to remember about functions:
- How to define them
- How to call/invoke them
# Function definition
def square(x):
return x * x
# Function invocation
print square(3)
# OR
input = 3
input_squared = square(input)
print input_squaredIn python, indentation is very important
Other languages, like C/C++ or Java use curly braces '{}' to denote when functions start and end
int square(int x) {
return x * x;
}In python, the function definition lasts as long as you keep the line indented
Functions can return a different type than their arguments
def number_to_string(in):
return '%s' % in