Functions in Python🤗


    Functions   

            Functions in programming are used to bundle a set of instructions that you want to use repeatedly or that, because of their complexity, are better self-contained in a sub-program and called when needed
         
            Function is a piece of code written to carry out a specified task. To carry out that specific task, the function might or might not need multiple inputs. When the task is carried out, the function can or can not return one or more values.

                There are three types of functions in Python:
  • Built-in functions, such as help() to ask for help, min() to get the minimum value, print() to print an object to the terminal,… 
  • User-Defined Functions (UDF 's), which are functions that users create to help them out; 
  • Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword.

        How To Define A Function: User-Defined Functions (UDFs)

        The four steps to defining a function in Python are the following:

      1. Use the keyword def to declare the function and follow this up with the function name.
      2. Add parameters to the function: they should be within the parentheses of the function. End your line with a colon.
      3. Add statements that the functions should execute.
      4. End your function with a return statement if the function should output something. Without the return statement, your function will return an object None.
                
                Example 1:

                def hello():
                  print("Hello World") 
                  return  
            
            hello()  ---> #function call
            
            # OUTPUT
            
            Hello World
            
            Example 2:
            
            def hello():
                  name=input("Enter your Name")
                  if  name:
                        print("Hello "+ name)
                  else:
                        print("Hello World") 
                  return
            hello()
        
            In the above function, we ask the user to give a name. If no name is given, the function will print out “Hello World”. Otherwise, the user will get a personalized “Hello” response with their name.

            Using return
                    We can also use return in the place of print the only difference is that the code written inside the function after return will not be executed. and you have to use print() while calling the function.

return() ---> in Python

            
        Example:

            def myfun(a):
                return a**2
            
            myfun(10)
            
        Output:
        
            100
            
        you can also use more than one arguments at the same time.

      Example
    
    def sum(a,b):
        return( a+b )
        print(sum(5,10))

    OUTPUT    

    15

            Default arguments:

            return( a+b )
       print(sum(10))
          
        15

    Keyword arguments:

                    Keyword arguments are how you call a function. Default values are how a function is defined. Default arguments mean you can leave some parameters out. Keyword arguments mean you don’t have to put them in the same order as the function definition.
        Example:
                def key(a=5,b=7):
                    c=a*2
                    d=b*3
                    return c,d
                print(key())
            Output
                (15,21)

                Arbitrary arguments:

        Example
        def sq(*a):
            return a**2
        print(sq(1,2,3,4,5))
    
     Output
      
         (1,4,9,16,25)

        Lambda function:

Lambda Functions



x = lambda a : a + 10
print(x(5))
15x= lambda a: a**2
print(x(7))
49

Comments