Function
Sections
- What are Functions?
- Why Use Functions?
- Defining a Function
- Call a function
- Arguments
- Function return
- Define a function with unlimited inputs
- Function Types
Section1- What are Functions?
Def: Functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code.The function is a block of code that performs a specific task. So, if we need to perform multiple tasks in a single piece of code then we need to create multiple functions to build a complete solution.
Def: Python functions are reusable pieces of code that perform specific tasks. They allow for modular and organized programming, thereby making it easier to build, maintain, and scale large software projects. As a cornerstone of Python programming, understanding functions is essential for anyone venturing into the field of Data Science or Al.
Def: A function is a block of organized, reusable code that performs a specific task. Functions provide better modularity and facilitate code reusability.

Section 2- Why Use Functions?
Modularity: Break down complex tasks into smaller,
manageable sub-tasks.
-Reusability: Write code once and use it in multiple places. Maintainability: Easier to update and debug.
Section 3- Defining a Function
To define a function, you use the `def` keyword followed by the function name and parentheses ().In Python a function is defined using the def
def my_function():
print("Hello from a function")
Section 4- Call a function
After defining a function, you can 'call' it to execute the code it contains.To call a function, use the function name followed by parenthesis:
my_function()
Section 5- 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.
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Section 6- Function return
To return the output from the function we need to write the “return” statement as given below
def a(x,y):
return x+y
a(2,3)
Section 7- Define a function with unlimited inputs
# define a function (take max 2 inputs)
def add(x,y):
#add two numbers
print(f'Reuslt:{result}')
#define a function (Take unlimited inputs
def add (*args):
# initialize result at 0
result =0
# iterate over args tuple
for arg in arg:
result +=arg
#print the result
print (f'Result:{result}')
Section 8- Function Types
If you are not familiar with "lambda functions," they are basically the same as "regular function but can be written more compactly as a one-liner.
def some_func(x): return 'Hello World ' + str(x) some_func(123)
'Hello World 123'
f = lambda x: 'Hello World ' + str(x) f(123)
'Hello World 123'