Python: Functions

Functions in python are defined using the block keyword “def”, followed with the function’s name as the block’s name. For example:

def my_function():
    print "Hello From My Function!"

Functions may also receive arguments (variables passed from the caller to the function). For example:

def my_function_with_args(username, greeting):
    print "Hello, %s , From My Function!, I wish you %s"%(username, greeting)

Functions may return a value to the caller, using the keyword- ‘return’ . For example:

def sum_two_numbers(a, b):
    return a + b

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):

# print a simple greeting 

#prints - "Hello, John Doe, From My Function!, I wish you a great year!"
my_function_with_args("John Doe", "a great year!")

# after this line x will hold the value 3!
x = sum_two_numbers(1,2)
Stackoverflow Solutions

Just started! Have not answered any questions.