Functions in Python

--Originally published at Ed_Alita

what huh cece hannah simone new girl

The elemental thing to understand this topic is to define what a function is. It can be define as a block organized, reusable code that is used to perform a single, related action. The language of python by default is gave you a lot of functions but you can make your own functions.  Let’s see how we can do it:

1 Step

Define a function

In python you can define function to be use in you code. The  rules to make these are the followings:Geek & Sundry stranger things law rules rule

  • The functions begin with def followed by the function name and parentheses ().
  • Any input parameters or arguments should be placed within these parentheses.
  • The first statement of a function can be an optional statement.
  • The code block within every function starts with colon (:) and is indented.
  • The statement return exits a function, optionally  passing back an expression to the caller. A return statement with no argument is the same as return none

Syntax

def function-name( parameters ):
   "function_doc string"
   function_suite
   return [expression

Example

The following function take the parameter string and then print it

Screenshot from 2016-08-22 19-50-02

2 Step

Calling a Functionexcited great awesome oprah exciting

Defining a function only gave it’s name an the parameters of it. So how the hell I use this thing that I create.

To call a function is simple you only put the name and the parameters let’s see with printme().

Screenshot from 2016-08-22 19-50-21

The result of calling by this way is the next one.

Screenshot from 2016-08-22 19-51-57

Function with multiple arguments

This example is only to show you that you can have more than one argument inside a function.

Screenshot from 2016-08-22 19-50-43

This function requires two arguments therefore is more complex. The age can be initialized by any value in this case I use 35 only for the lols.

The result of calling it and running it is the

Screenshot from 2016-08-22 19-51-19
Screenshot from 2016-08-22 19-52-17
one.

Screenshot from 2016-08-22 19-51-19

Screenshot from 2016-08-22 19-52-17

All the material cover in this post can be found in the next link.

http://www.tutorialspoint.com/python/python_functions.htm