Python functions
# Functions are one of the most powerful parts of programming - they can perform all sort of - well - functions, basically.
# Functions can look for particular things within content, perform calculations, create new objects, alter lists and dictionaries, and lots else besides.
# Essentially they are pieces of script within the script, and as such can be run repeatedly until a particular condition is met (e.g. something is found or a number is reached)
# They can also be left unused unless a particular condition is met
# Like so many things in programming, the main advantage of a function is that it saves you time by allowing you to run one piece of code over and over again
#Functions are created with the word def as follows: def functionname():
#the parenthesis contains any 'arguments' that are passed to the function - that is, any information which the function might use, such as a starting number, string (text), variable or state (such as true or false).
#the colon is needed to begin building the function in the lines that follow, which should be indented
def multiply_me(first_number, second_number):
third_number = first_number * second_number
print third_number
# In this example the function 'multiply_me' expects to be supplied with 2 arguments (in the brackets)
# these arguments are given variable names at the same time (in the brackets) - first_number and second_number
# a new variable is created - third_number. In the same line it is given a value: the result of multiplying first_number by second_number
# finally, that new variable is printed (displayed to the user)
# However, for a function to actually run it has to be called. This is done in a script as follows:
multiply_me(200, 300)
# That line sets the computer looking for the multiply_me function within the script
# It also passes the two numbers within the brackets to the function
# The function then runs, taking in those numbers and using them to make the calculation, and print it.
# If you were calling a function which did need any information to work, you would simply leave the brackets empty:
multiply_me()
# In this case, because the function does need two arguments to work, you would get an error saying how many arguments it takes
#Python has many built-in functions that you do not need to create from scratch - including functions that will convert numbers into binary. These are listed in Python's documentation.