Skip to content

Latest commit

 

History

History
77 lines (61 loc) · 1.78 KB

File metadata and controls

77 lines (61 loc) · 1.78 KB

Function Definiton

def helloFunction():
    print( "helloFunction() => ", "Hello World" )

helloFunction()

# => helloFunction() =>  Hello World

Python functions can have optional docstring

def docFunction():
    """
    This is a documentation line for the docFunction.
    This can help user understand what the function does.
    """
    print( "docFunction() => ", "I am important!" )

print( docFunction.__doc__ )
# =>    This is a documentation line for the docFunction.
# =>    This can help user understand what the function does.

Python function can take arguments

def printGreeting( userName ):
    print( "printGreeting() => ", "Hello {}!".format( userName ) )

printGreeting( "John Doe" )
# => printGreeting() =>  Hello John Doe!

Python function can return values

# any code after return statement is ignored without warning or an error
def sumTwoNumbers( num1, num2 ):
    return num1 + num2

print( "sumTwoNumbers(11, 22) => ", sumTwoNumbers(11, 22) )
# => sumTwoNumbers(11, 22) =>  33

Nesting functions

def squareOfSum( num1, num2 ):
    def squareNum( input ):
        return input * input

    sumResult = num1 + num2
    squareResult = squareNum( sumResult )
    return squareResult

print( "squareOfSum(2, 3) => ", squareOfSum(2, 3) )
# => squareOfSum(2, 3) =>  25

Defining function in one line

# you can use multiple statements separated by `;`
def squareMe( num ): return num * num

print( "squareMe(2) => ", squareMe(2) )
# => squareMe(2) =>  4

Use of pass keyword

def toDoFunction():
    pass
toDoFunction()
# =>