Functions in Python

A function is a block of code that can be called from another location in the program or class.

#function definition
def function_name(passed_params):
    #code to be executed
    return value #optional return statement

#function call

#function call with associative parameter variables
function_name(param1=value1, param2=value2)


It is used to reduce the repetition of multiple lines of code.

A function can either return nil, a value, or an array of values.

When declaring a function, parameters can be passed as regular variables (no prefix to variable), tuples for a variable number of args (using a single asterisk prefix *), or as a dictionary (using a double asterisk prefix **).

By default, functions do not have access to outside variables. To access global variables, use the 'global' keyword in front of the variable name. Otherwise, these global variables will be initialized inside the function with local scope.

def find_minimum(a, b):
    if a < b:
        return a
        return b

#function call
min_number = find_minimum(5, 10) #should return 5

< Generators   |   First Class Functions >

© 2019 SyntaxDB. All Rights Reserved.