Functions in Python

walden systems, geeks corner, programming, languages, Python, scope, local scope, global scope, hoisting, functions, variables, developer, scripting, decrypt, aes256, encrypt, pycrypto, pip, tuple, dictionary, list, file, read, write, readline, while, for, loops, break, continue, udf, functions, argument, return value
Python is a programming language that lets you work quickly and integrate systems more effectively.

Functions are an essential part of a programming language. We have already used some of functions built-in and may have used some functions that come with its library. We use functions to bundle a set of instructions that we want to use repeatedly or because of their complexity, are better self-contained in a sub-program and are called when needed. What that means is that a function is a piece of code written to execute a specific task. To carry out that task, functions may o may not need multiple inputs. When the task is carried out, the function may return one or more values.

There are three types of functions in Python. The first is built-in functions such as print( ) to print an object to the terminal. Next, are user defined functions which are functions that we, as programmer create. And lastly, anonymous functions which are also called lambda functions because they are not declared with the standard def keyword.

Defining a function

Defining a function consists of some basic components. The first is marked by the def keyword which is the beginning of the function header. Next comes the function name which is unique to identify it. Next comes arguments which are optional and are used to pass values to the function. The end of the function header is marked with a colon ( : ). Next, we can add an optional docstring to describe what the function does. The code block comes next and makes up the function body and have the same indentation, usually 4 spaces. Finally, we can have a return statement to return value(s) from the function.

def function_name( arguments ) :
    """docstring"""
    statement(s)
    return [ return_value ]


Example of a function
def print( name ) :
    """This function prints hello and the argument name"""
    print("Hello, " + name )


Arguments

There are four types of arguments that Python UDFs can take. The first type is the default arguments which take a default value if no argument value is passed during the function call. You can assign this default value by with the assignment operator =. Required arguments are arguments that have to be passed during the function call and have to be in the exact order in which the function was defined with. Keyword arguments can be used if you want to make sure that you call all the parameters in the right order by identifying the arguments with the argument name. The last type is variable arguments which are used if we don't know the number of arguments we want pass using the *args syntax.

1    def myAdd(a,b = 2):
2        return ( a + b )
3  
4    myAdd( a=1 )
5
6    myAdd( a=1, b=3 )
7    myAdd( b=4, a=1 )


The argument a is a required argument, the argument b is an optional argument in line 1.

The function calls in lines 6 and shows how to call functions with keyword arguments, note that the order of the arguments doesn't matter.


def mySum( *args ):
  return sum( args )

mySum( 1,2,3 )


Example of calling a function with a variable number of arguments.

Return values

A Python function can return exactly one object. An object can be a numerical value, like an integer or a float but can also be a list or a dictionary. if we have to return multiple values, we can return a tuple. This is how we return multiple values. Remember that this data structure is very similar to that of a list, it can contain multiple values. However, tuples are immutable, which means that we can't modify any values that are stored in it. We construct tuples with the help of double parentheses (). We can unpack tuples into multiple variables with the help of the comma and the assignment operator.

def myAdd( a, b ):
  sum = a + b
  return ( sum, a )
 
sum, a = myAdd( 3, 4 )
print( sum )