You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Theo Chupp edited this page Jul 21, 2017
·
3 revisions
Functions
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 definitiondefsquare(x):
returnx*x# Function invocationprintsquare(3)
# ORinput=3input_squared=square(input)
printinput_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
intsquare(intx) {
returnx * x;
}
Functions can have multiple parameters.
defmultiply(x, y):
z=x*yreturnzprintmultiply(3, 5) # Prints 15
Functions can be invoked inside other functions
They can also have expressions as arguments
defsquare(x):
returnmultiply(x, x)
printsquare(multiply(2, 3)) # Prints 36