-
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 special blocks of code that can take input and give 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_squared
When invoked, they are evaluated to expressions
They can have zero to many inputs, and an optional output
In python, indentation is very important
A function definition lasts as long as you keep the line indented
Other languages, like C/C++ or Java, use curly braces '{}' to denote when functions start and end
int square(int x) {
return x * x;
}
Functions can have multiple parameters.
def multiply(x, y):
z = x * y
return z
print multiply(3, 5) # Prints 15
Functions can be invoked inside other functions
They can also have expressions as arguments
def square(x):
return multiply(x, x)
print square(multiply(2, 3)) # Prints 36