# Python Functions and Lambda Functions Day - 12

**Functions:**

* it's a block of code which is executed only if it is called, it can have any number of parameters to pass the data.
    
* use **def** keyword to define a function and **return** inside the function to return a value.
    
* Any data type can be sent as an argument to the function.
    
* **Default Arguments:** Allows to define a function with default value assigned to one or more arguments. It uses default value if no value is passed, else the value gets overridden if any value is passed.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719060584072/6cbc13e2-8636-4e12-97a0-ace3ac2dadbb.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719060604419/1aad5355-9c0f-43ad-b691-a6cbd5402ee1.png align="center")
    
* **Arbitrary Arguments:** If the number of arguments are not known, just place \* before the parameter in function definition. (\**args*)
    
    * These arguments can be Positional or Keyword Arguments.
        
    * For Positional Arguments, an argument is prefixed with single asterisk (\*) and it can store any number of values as shown in the example below.
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719235692531/ed776a94-d30b-4e27-bf08-0ffb5a213a5f.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719235711318/357eb7b1-9199-47bd-9c8f-07103124214f.png align="center")
        
    * For Keyword Arguments, an argument is prefixed with double asterisk(\*\*) and that is stored as Dictionary(Key Value Pair)
        
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719240821991/1c0322a7-7823-480b-b6bd-293e798ea6a3.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719240841545/eec15396-2d79-4cb4-a8b4-f5b6d76f0663.png align="center")
    
* **Keyword Arguments:** Allows to pass function arguments in the form of keywords, which are also known as 'Named Arguments'. Arguments with *key = value* can be sent. Arbitrary Keyword Arguments (\*\*args)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719062927479/e506d47f-2d5a-4eb6-97a9-3ef34de4253a.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719062999141/37702c7f-d481-47fc-99e6-cf0b58694903.png align="center")
    
    **NOTE** : Always the Positional Arguments(from above example, person('Ram','Hyderabad')) must be before the Keyword Arguments(from above example, person(name = 'Sita', location = 'Canada')).
    
* **Positional-Only Arguments**: Use **, /** after the arguments.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719242941140/29b9d6e2-c08f-4a47-aeef-040870c7984d.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719242960026/0ff08e02-d66a-4d3a-a19c-01e098540e1f.png align="center")
    
* **Keyword-Only Arguments:** Use **\*,** before the arguments.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719243003334/2e1020ba-3828-4066-b23d-8d6fd54e90ae.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719243023564/4418af14-0be8-4a04-802f-c989321f1a3b.png align="center")
    
* **Default Parameter Value:** If a function is called without argument, then it uses default value.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719242255812/2a1263d4-10ca-42f0-bd14-8b5437e6bf80.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719242275941/cea4171d-b879-49b2-bde5-00d27874d96b.png align="center")
    
    **\*Lambda Function:** It is an anonymous function.
    
* It can have any number of arguments, but can have only one expression.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719248747553/0729c60d-63d7-4c97-bdde-831c501664e9.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1719248766723/a4bffc4e-3f49-4712-89fe-3b620ade75e2.png align="center")
    
    | **lambda function** | **function** |
    | --- | --- |
    | Supports single-line statements that return some value. | Supports any number of lines inside a function block |
    | Good for performing short operations/data manipulations. | Good for any cases that require multiple lines of code. |
    | Using the lambda function can sometime reduce the readability of code. | We can use comments and function descriptions for easy readability. |
