--Originally published at Ed_Alita
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:
- 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
2 Step
Calling a Function
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().
The result of calling by this way is the next one.
Function with multiple arguments
This example is only to show you that you can have more than one argument inside a function.
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
Continue reading "Functions in Python" →