<jupyter_start><jupyter_text>Functions in python

Functions are series of steps or instructions to complete a specific task

The advantages of using functions in python programming are:

- They **increase readability**, particularly for longer codes, since a function can be called at any point in the program
- They **reduce code length**, since the same code is not repeated in different places
- They allow **reuse of code**  Types of Functions

 1) Built-in Function

- These are the functions that are ready-made in python, and are frequently used in many programs.
- These do **not** need to be imported<jupyter_code>print("hello")
input("hello")
for i in range(10):
    print(i)<jupyter_output>hello
0
1
2
3
4
5
6
7
8
9<jupyter_text>2) Module Functions

- The python standard libraries consists of collections functions other than inbuilt ones, which can be used through **modules**
- Modules are a **group of functions** that can be imported into a program by the import statement and can be used accordingly.
- Modules have a `.py` extension
- In fact, when you make a file such as `code.py` you are making a module!<jupyter_code>import math
print(math.sqrt(25))
print(math.log(2))<jupyter_output>5.0
0.6931471805599453<jupyter_text>Ways to import modules
- import module_name

    To call a function in a module imported this way, you must write the **name of the module** before it<jupyter_code>import random
print(random.randint(2,10))<jupyter_output>4<jupyter_text>- import _modulename_ as _newname_

    In this method, you can import a module with **any name you want** and you can use that name before calling a function

    This new name is known as an **alias**  and it points to the module object in the memory.<jupyter_code>import statistics as st
st.mean([2,3,4,5,6])<jupyter_output><empty_output><jupyter_text>- from _modulename_ import _function1_,_function2_,...

    In this method, **you don't need to specify the modules name** before calling a function, since you will be importing specific functions from it

    To import **all the functions** from a python module, you can simply use an * instead of the function names<jupyter_code>from math import *
print(fabs(-10))
print(sqrt(9))
print(log(e))<jupyter_output>10.0
3.0
1.0<jupyter_text>3) User Defined Functions

These are functions the **user can define** and **use** in their programs.<jupyter_code>def sum(a,b):
    print(a+b)

sum(2,5)<jupyter_output>7
{'fraction_non_alphanumeric': 0.08397880146759071, 'fraction_numerical': 0.024867509172441907, 'mean_word_length': 4.801418439716312, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '509736', 'license': 'MIT', 'n_tokens_mistral': 761, 'n_tokens_neox': 747, 'n_words': 321, 'path': 'functions/1)functions_intro.ipynb', 'repo_name': 'qprogramming-net/cbse-xii', 'size': '2453'}