<jupyter_start><jupyter_text>**Python Functions**In this kernel, we will learn about Python functions; what is a function, the syntax, components and types of a function. Also, we will learn to create a function in Python.  **1. What is a function in Python**- In Python, function is a group of related statements that perform a specific task.- Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.- Furthermore, it avoids repetition and makes code reusable. - A function is a block of code which only runs when it is called.- You can pass data, known as parameters, into a function.- A function can return data as a result.  **2. Syntax of Function**<jupyter_code>def function_name(parameters):
        """docstring"""
        statement(s)<jupyter_output><empty_output><jupyter_text>**2.1 How Function works in Python?** ![How Function works in Python](https://cdn.programiz.com/sites/tutorial2program/files/python-how-function-works_1.jpg)  **3. Creating a Function**- 1 In Python a function is defined using the def keyword followed by the function name and parentheses (()).- 2 Keyword def marks the start of function header.- 3 A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python.- 4 Parameters (arguments) through which we pass values to a function. They are optional.- 5 A colon (:) to mark the end of function header.- 6 Optional documentation string (docstring) to describe what the function does.- 7 One or more valid python statements that make up the function body. Statements must have same indentation level (usually 4 spaces).- 8 An optional return statement to return a value from the function.[Python Keywords and Identifiers](https://www.programiz.com/python-programming/keywords-identifierrules)  **4. Example of Functions**  **4.1 Example 1**<jupyter_code>def my_function():
    print("Hello from a function")<jupyter_output><empty_output><jupyter_text>Call the function<jupyter_code>my_function()<jupyter_output>Hello from a function<jupyter_text>**4.2 Example 2**<jupyter_code>def ex2():
    x1=x2=5
    print("The sum of x1 and x2 is", x1+x2)
ex2()<jupyter_output>The sum of x1 and x2 is 10<jupyter_text>**5. How to define and call a function in Python**- Function in Python is defined by the `def` statement followed by the function name and parentheses (()).<jupyter_code>def func1():
    print("I am learning Python Function")  # function definition
    
func1()                            # function call<jupyter_output>I am learning Python Function<jupyter_text>![Function definition, call and output](https://www.guru99.com/images/Pythonnew/Python10.1.png) - The function call `func1()` calls our function `func1():` and print the command **"I am learning Python."**- There are some set of rules in Python to define a function.  - Any args or input parameters should be placed within these parentheses  - The function first statement can be an optional statement- docstring or the documentation string of the function.    - The code within every function starts with a colon (:) and should be indented (space).    - The statement return (expression) exits a function, optionally passing back a value to the caller. A return statement with no args is the same as return None.  **6. Docstring and input argument to a function**<jupyter_code>def greet(name):
    """This function greets to the 
    person passed in as parameter"""
    print("Hello " + name + ", Good Morning!")<jupyter_output><empty_output><jupyter_text>**6.1 How to call a function in Python**- Once we have defined a function, we can call it from another function, program or even the Python prompt. - To call a function we simply type the function name with appropriate parameters.<jupyter_code>greet("Prashant")<jupyter_output>Hello Prashant, Good Morning!<jupyter_text>**6.2 Docstring**- The first string after the function header is called the docstring and is short for documentation string. It is used to explain in brief, what a function does.- Although optional, documentation is a good programming practice. Unless you can remember what you had for dinner last week, always document your code.- In the above example, we have a docstring immediately below the function header. We generally use triple quotes so that docstring can extend up to multiple lines. This string is available to us as __doc__ attribute of the function.  **7. Significance of Indentation (Space) in Python**- Before we get familiarize with Python functions, it is important that we understand the indentation rule to declare Python functions and these rules are applicable to other elements of Python as well like declaring conditions, loops or variable.- Python follows a particular style of indentation to define the code, since Python functions don't have any explicit begin or end like curly braces to indicate the start and stop for the function, they have to rely on this indentation. - Here we take a simple example with "print" command. When we write "print" function right below the def func 2(): It will show an "indentation error: expected an indented block". ![Indentation Error](https://www.guru99.com/images/Pythonnew/Python10.2.png) - Now, when we add the indent (space) in front of "print" function, it should print as expected.<jupyter_code>def func1():
    print("I am learning Python Functions")

func1()<jupyter_output>I am learning Python Functions<jupyter_text>![Expected Output with no error](https://www.guru99.com/images/Pythonnew/Python10.3.png) - At least, one indent is enough to make your code work successfully. - But as a best practice it is advisable to leave about 3-4 indent to call your function. - It is also necessary that while declaring indentation, you have to maintain the same indent for the rest of your code. - For example, in below screen shot when we call another statement **still in func1** and when it is not declared right below the first print statement it will show an indentation error **unindent does not match any other indentation level.** ![Indentation Error](https://www.guru99.com/images/Pythonnew/Python10.4.png) - Now, when we apply same indentation for both the statements and align them in the same line, it gives the expected output. ![Same indentation for the statements](https://www.guru99.com/images/Pythonnew/Python10.5.png)  **8. How Function Return Value - with Return Statement**  **8.1 A Simple Example**<jupyter_code>def square(x):
    print(x*x)  
square(4)<jupyter_output>16<jupyter_text>**8.2 Return Command in Python**- Return command in Python specifies what value to give back to the caller of the function.- Let's understand this with the following example. - Here - we see when function is not "return". - For example, we want the square of 4, and it should give answer "16" when the code is executed. Which it gives when we simply use **print x x** code, but when you call function **print square** it gives **None** as an output. - This is because when you call the function, recursion does not happen and fall off the end of the function. Python returns **None** for failing off the end of the function.<jupyter_code># define return function
def square(x):
    print(x*x)
    
print(square(4))<jupyter_output>16
None<jupyter_text>![FUnction returns None](https://www.guru99.com/images/Pythonnew/Python10.6.png) - To make this clearer we replace the print command with assignment command. Let's check the output. - When you run the command **print square (4)** it actually returns the value of the object since we don't have any specific function to run over here it returns **None**.<jupyter_code>def square(x):
    y = x*x
    
print(square(4))<jupyter_output>None<jupyter_text>![Function returns None](https://www.guru99.com/images/Pythonnew/Python10.7.png) - Now, here we will see how to retrieve the output using **return** command. When you use the **return** function and execute the code, it will give the output "16."<jupyter_code>def square(x):
    return x*x
    
print(square(4))<jupyter_output>16<jupyter_text>![Use of Return Command](https://www.guru99.com/images/Pythonnew/Python10.8.png) - Functions in Python are themselves an object, and an object has some value. - We will here see how Python treats an object. - When you run the command "print square" it returns the value of the object. - Since we have not passed any argument, we don't have any specific function to run over here it returns a default value (0x021B2D30) which is the location of the object. - **In practical Python program, you probably won't ever need to do this.**<jupyter_code>def square(x):
    return x*x
    
print(square)<jupyter_output><function square at 0x7fcd35028f28><jupyter_text>![Functions are themselves an object](https://www.guru99.com/images/Pythonnew/Python10.9.png)  **8.3 The return statement**- The **return statement** is used to exit a function and go back to the place from where it was called.- Syntax of return :   `return [expression_list]`   - This statement can contain expression which gets evaluated and the value is returned. If there is no expression in the statement or the return statement itself is not present inside a function, then the function will return the None object.<jupyter_code>print(greet("Prashant"))<jupyter_output>Hello Prashant, Good Morning!
None<jupyter_text>**8.4 Example of Return Statement**<jupyter_code>def absolute_value(num):
    """This function returns the 
    absolute value of the given number"""
    
    if num >=0:
        return num
    else:
        return -num<jupyter_output><empty_output><jupyter_text>**Call the above function**<jupyter_code># Output : 2

print(absolute_value(2))
# Output : -4

print(absolute_value(-4))<jupyter_output>4<jupyter_text>**9. Scope and Lifetime of variables**- Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.- Lifetime of a variable is the period throughout which the variable exits in the memory. The lifetime of variables inside a function is as long as the function executes.- They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.- Here is an example to illustrate the scope of a variable inside a function.<jupyter_code>def my_func():
    x=10
    print("Value inside function:", x)
    
x = 20
my_func()
print("Value outside function:", x)<jupyter_output>Value inside function: 10
Value outside function: 20<jupyter_text>- Here, we can see that the value of x is 20 initially. Even though the function my_func() changed the value of x to 10, it did not effect the value outside the function.- This is because the variable x inside the function is different (local to the function) from the one outside. Although they have same names, they are two different variables with different scope.- On the other hand, variables outside of the function are visible from inside. They have a global scope.- We can read these values from inside the function but cannot change (write) them. In order to modify the value of variables outside the function, they must be declared as global variables using the keyword global.  **10. Arguments in Functions**- The argument is a value that is passed to the function when it's called.- In other words on the calling side, it is an argument and on the function side it is a parameter.- Let see how Python Args works - - Arguments are declared in the function definition. While calling the function, you can pass the values for that args as shown below<jupyter_code>def multiply(x,y):
    print(x*y)
    
multiply(2,8)<jupyter_output>16<jupyter_text>![Arguments in Functions](https://www.guru99.com/images/Pythonnew/Python10.10.png)  **Default value of an argument**- To declare a default value of an argument, assign it a value at function definition as follows- ![Default value of an argument](https://www.guru99.com/images/Pythonnew/Python10.11.png) - x has no default values. - Default values of y=0. - When we supply only one argument while calling multiply function, Python assigns the supplied value to x while keeping the value of y=0. - Hence the multiply of x*y=0<jupyter_code>def multiply(x,y=0):
    return x*y

print(multiply(4))<jupyter_output>0<jupyter_text>![Default value of y=0](https://www.guru99.com/images/Pythonnew/Python10.12.png) - This time we will change the value to y=2 instead of the default value y=0, and it will return the output as (4x2)=8.<jupyter_code>def multiply(x,y=0):
    return x*y

print(multiply(4,y=2))<jupyter_output>8<jupyter_text>![Default value of argument changed](https://www.guru99.com/images/Pythonnew/Python10.13.png) - You can also change the order in which the arguments can be passed in Python. Here we have reversed the order of the value x and y to x=4 and y=2.<jupyter_code>def multiply(x,y=0):
    print("Value of x=",x)
    print("Value of y=",y)
    return x*y

print(multiply(y=2,x=4))<jupyter_output>Value of x= 4
Value of y= 2
8<jupyter_text>![Change the order of arguments](https://www.guru99.com/images/Pythonnew/Python10.14.png) - Multiple Arguments can also be passed as an array. Here in the example we call the multiple args (1,2,3,4,5) by calling the (*args) function.- Example: We declared multiple args as number (1,2,3,4,5) when we call the `(*args)` function; it prints out the output as (1,2,3,4,5)<jupyter_code>#passing multiple arguments
def guru99(*args):
    
    print(args)
    
guru99(1,2,3,4,5)<jupyter_output>(1, 2, 3, 4, 5)<jupyter_text>![Multiple Arguments in Function](https://www.guru99.com/images/Pythonnew/Python10.15.png)  **10.1 Arguments**- Information can be passed into functions as arguments.- Arguments are specified after the function name, inside the parentheses. - You can add as many arguments as you want, just separate them with a comma.  **Example of function with argument**- The following example has a function with one argument (fname). - When the function is called, we pass along a first name, which is used inside the function to print the full name.<jupyter_code>def my_function(fname):
    print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")<jupyter_output><NAME><jupyter_text>**Arguments are often shortened to args in Python documentations.**[Python Functions](https://www.w3schools.com/python/python_functions.asp)  **Parameters or Arguments?**- The terms **parameter** and **argument** can be used for the same thing: information that are passed into a function.- From a function's perspective:  - **parameter** - A parameter is the variable listed inside the parentheses in the function definition.  - **argument** - An argument is the value that are sent to the function when it is called.  **Number of Arguments**By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.  Example- This function expects 2 arguments, and gets 2 arguments:<jupyter_code>def my_function(fname, lname):
    print(fname + " " + lname)
    
my_function("Emil", "Refsnes")<jupyter_output><NAME>
{'fraction_non_alphanumeric': 0.08473913893050218, 'fraction_numerical': 0.016999934866149938, 'mean_word_length': 4.665682656826569, 'pattern_counts': {'":': 0, '<': 80, '<?xml version=': 0, '>': 81, 'https://': 18, 'lorem ipsum': 0, 'www.': 17, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '24', 'dir': 'jupyter-structured-clean-dedup', 'id': '344391', 'license': 'CC0-1.0', 'n_tokens_mistral': 4363, 'n_tokens_neox': 4146, 'n_words': 2000, 'path': 'Python_Functions.ipynb', 'repo_name': 'pb111/Python', 'size': '15366'}