Skip to content

Latest commit

 

History

History
53 lines (41 loc) · 1.08 KB

Function.md

File metadata and controls

53 lines (41 loc) · 1.08 KB

Function

User-Defined Function

Parameter vs Argument

  • Parameter: Defined with function
  • Argument: Passed as value during the function call.
def function(parameter1, parameter2):
    """
    This is a Doctstring
    """
    print("The Message")

greet(argument1, argument2)

Arbitrary Arguments

  • When we don't know how many arguments we will pass.
def greet(*people):
    """
    Docstring
    parameters (data type): about parameter
    """

    # people is an iterator with arguments (list, tuple, set, string)
    for name in people:
        print("Hello", name)

greet("Kirankumar", "Paramveer", "Gaurav", "Pranit")

Global Keyword

  • Variable created inside a function is a local variable by default.
  • Variable created outside a function is a global variable by default.
  • Keyword gobal is used to declare global variable inside a function.
# global variable:
c = 1 

def add():
    # use of global keyword:
    global c
    # increment c by 2:
    c = c + 2 
    print(c)

add()