Using functions
- python allows to call functions by passing arguments
- Consider the following function
def add(a: int|float, b: int|float) -> int|float:
"""Returns sum of two numbers
Args:
a (int|float):
b (int|float):
Returns:
int|float: Return sum of two numbers
"""
return a + b
- Calling arguments by position
add(1,2)
- Calling arguments by name
add(b=2, a=1)
- Optional arguments: i.e. arguments with default values
- Refer Here for the notebook with functions examples added
Create a function
- Lets write a program which prints n = 3
*
***
*****
*
***
*****
*******
*********
def print_triangle(size=5,symbol='*'):
index = 0
while index < size:
count = 2*index + 1
space_count = size - index - 1
print(" "*space_count, end="")
print(symbol*count)
index = index + 1