Why you should care about functions.

--Originally published at Carolina's Blog Site

A function is a named sequence of statements that performs an operation/computation. Functions may or may not take arguments (which are like the ingredients that change depending on what result you want to achieve in that particular occasion).

To define a function,

  • you write def followed by the name of the function,
  • then you write the parameters/arguments in () and you end with a “:”
  • In the body of the function, which has to be indented, you type the order of the statements you want that function to do.
  • To finish off a function, type return and specify what the function will return. If you don’t want to return anything, just leave the return line blank with (). Any code after the return statement will not happen.

Later, you can “call” the function by its name, and just type the parameters for that occasion. I know, awesome!

Side note:

  • Parameters can have a default value, for example: (parameter1, parameter2, parameter3=55).
  • If a function is a thing and parameters can be anything…then functions can also be used as parameters of other functions.

 

pic souce: http://www.relatably.com/m/functional-programming-memes