What is function ?
A function is an reusable block of code or instruction to perform a specific task with their name.
Function is also called as procedure/sub procedure/method in other programming languages.
why do learn function in python ?
- Code reusability.
- Easy Maintenance --> modification done easly.
- Code Readability.
- Modularity --> better Organized code.
- Easy testing and debugging.
Function structure
- To define function , def keyword to be used.
- The function name should be in verb.
- Function call must be done after the function declaration
1) Basic Structure:
def greet():
print("Welcome")
greet()
2) Passing Inputs
Arguments and Parameters
- While calling the function we can send an input is named as Arguments.
- While receiving input(value) were the function is declared is named parameters.
def add(no1 , no2): #no1 ,no2 receving the value called parameters
print(no1 + n02)
add(10, 20) # 10 , 20 were Arguments
3) Return function
- To send result to back were the function called , we can use return keyword.
- If a function does not have a return statement, it returns None by default.
def add_numbers(x, y):
return x + y
result = add_numbers(5, 3)
print(result)
Function overloading
- Same function name with different number of arguments or different types of arguments is called function overloading.
def add(no1, no2):
print(no1 + no2)
def add(no1, no2, no3):
print(no1 + no2 + no3)
add(100,200)
add(5,10,15)
Top comments (0)