Functions

What are Functions?

Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.

How do you write functions in Python?

As we have seen on previous tutorials, Python makes use of blocks. A block is a area of code of written in the format of:
block_head:
    1st block line
    2nd block line
    ...
#sample function definition
def my_function():
     print "Hello From My Function!"

How do you call functions in Python?

Simply write the function's name followed by (), placing any required arguments within the brackets. For example, lets call the functions written above (in the previous example):
def my_function():
     print "Hello From My Function!"
my_function()
Note: Copy and Paste Code into Compiler
 Open Compiler 

PreviousNext