# Functions

## Defining Functions

* For defining a function in python we use def(define) followed by function\_name() and colon
* Make sure that the function name is meaningful and descriptive
* Function names are generally given in snake\_case in python

```python
def greet():
    print("Hi there")
    print("Welcome abroad")
    
greet()
```

## Arguments and Parameters

* A parameter is an input that we define for our function&#x20;
* In the below example, first\_name and last\_name are parameters&#x20;
* While an argument is an actual value for a given parameter ("Ankit", "Choudhary" are arguments)

```python
def greet(first_name,last_name):
    print(first_name + " " + last_name))
    print("Welcome aboard")  
    
greet("OoBA","Labs")
```

## Types of Functions

### Perform a Task

* Below is a sample function in python
* In Python, all functions by default return None values

```python
def greet(first_name,last_name):
    print("Hi {} {}".format(first_name,last_name)) #Python2
    #print(f"Hi {first_name} {last_name}") #Python3
    print("Welcome aboard")
       
greet("OoBA","Labs")
```

### Return a Value

```python
def greet(first_name,last_name):
    return first_name + " " + last_name

full_name = greet("Ankit","Choudhary")
print(full_name) # prints "Ankit Choudhary"
```

## Arguments in Function

* Specifying the name of a parameter with an argument is called a keyword argument
* For example: "number=1" is a keyword argument in the next example

### Keyword Argument

```python
def increment(number,by):
    return number + by

print(increment(number=1,by=2)) # prints 3
```

### Default Argument

* All the parameters which we define for a function are required by default
* We can make a parameter optional by specifying the default value while defining the function&#x20;
* In the below given example "by" is having 1 as default value if no value is passed as an argument
* All the optional parameters must be defined after the required parameters.

```python
def increment(number,by = 1):
    return number + by

print(increment(number=1)) # print 2
```

### Xargs, Variable number of arguments

* We can pass a variable number of arguments to a function by using \* before the parameter name
* The variable arguments will be stored in a tuple and can be iterated.
* **Note that** variable arguments parameter should be at the end of function parameters

```python
def multiply(x,*numbers):
    print(x) # prints 2
    print(numbers) # prints (3,4,5)
    total = 1
    for number in numbers:
        total *= number
    return total
    
print(multiply(2,3,4,5)) # prints 60
```

### XXargs

* We can also pass multiple "key: value" pairs by using \*\* before the parameter name
* **Note that** variable arguments parameter should be at the end of function parameters

```python
def save_user(number,**user):
    print(number) # prints 1
    print(user) # prints dictionary with all key value pairs
    print(user["name"]) # "John"
        
save_user(1,id =1, name="John", age=22)
```

## Scope of a Function

```python
def greet(name):
    message = "a"
        
def send_mail(name):
    message = "b"
```

### Global Variable

* A Global variable is will be accessible throughout the file and inside each and every function
* Global variables stay for a longer period of time in a program until it is garbage collected,  so we should not use them often
* If the same variable is defined in a function's scope, the local variable will override the global variable
* Here in function greet, the global variable "message" will be used While in the function send\_mail as message variable is defined locally so that will be used.

```python
messge = "global variable"
def greet(name):
    print(messge)
        
def send_mail(name):
    message = "local variable"
    print(message)
    
greet("OoBA")
send_mail("OoBA")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ankit-apdc.gitbook.io/python-1/part-1-basics/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
