Functions and arguments

FUNCTIONS

Functions are blocks of organized, reusable code used to perform single or related set of actions. They provide better modularity and high degree of reusability. Python supports two types of functions:
1.    Built in functions – Functions that are available in the Python Interpreter and available for use in all programs.
2.    User defined functions – Functions defined by users to perform specific task. 

Function definition
1.    Function definitions start with keyword ‘def’ followed by ‘function name’, parenthesis () and a colon
2.    Arguments are placed inside these parenthesis
3.    Every line inside code block is indented.
4.    Return statement exits the function
5.    Return statements can be written with expression (returns an expression to the caller function) or without expression (returns none)

Syntax:
def function_name(parameters):
          # optional comment statement for documentation
          function_suite        # code block
          return [expression]

Note parameters exhibit positional behavior. Hence should be passed in the same order as in function definition.

Different types of formal arguments
1.    Required arguments
2.    Keyword arguments
3.    Default arguments
4.    Variable length arguments



Different types of formal arguments
1.    Required arguments
                         i.    Arguments follow positional order
                       ii.    Number of arguments and order of arguments in function call should be same as in function definition
2.    Keyword arguments
                         i.    When used in function call, the calling function identifies the arguments by parameter name
                       ii.    Allows to skip arguments or place them out of order
                      iii.    Python interpreter uses the keyword to match the values with the parameters

Example:

# Functions and Arguments
# Required and keyword arguments

def fn_display(x, y):
        print("x =", x)
        print("y =", y)
        return
         
num1 = int(input("Enter number : "))
num2 = int(input("Enter number : "))

# Required arguments ( x = num1, y = num2)
print("Required arguments:")
total = fn_display(num1, num2)

# Keyword arguments
print("Keyword arguments:")
total = fn_display(y = num1, x = num2)

Output:
Enter number : 7
Enter number : 5
Required arguments:
x = 7
y = 5
Keyword arguments:
x = 5
y = 7

3.    Default arguments
                         i.    Assumes a default value if the value is not specified for that argument in the function call

Example

# Functions
# default arguments
def fndisplay(y, s = "user", x = 5):
       print(y, s, x)
       return

                                                    # Output
fndisplay(7)                                # 7 user 5
fndisplay(7, "IT")                       # 7 IT 5
fndisplay(7, "IT", 25)                 # 7 IT 25

4.    Variable length arguments
                         i.    Used to execute functions with more arguments than specified during function definition
                       ii.    Unlike required and default arguments, variable arguments are not named while defining a function.
                      iii.    An ‘*’ is placed before variable names to hold all non keyword variable arguments

Example

def fndisplay(*t):
          for ele in t:
                   print(ele, end="  ")
          return
                                                                  
fndisplay(1,2,3,4,5)
fndisplay(1, 2.5, "asd", True, (2+5j))

Output:
1  2  3  4  5
1  2.5  asd  True  (2+5j)

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu