In python a function can call itself.
To be avoided while using recursion:
- Writing a never ending program.
- Writing a program which uses excessive power.
- Writing a program which uses excess memory.
if(m > 0):
result = m + tri_recursion(m – 1)
result = 0
print(“\n\nRecursion Example Results”)
A return value returns function value.
return 10 * x
We can send any data type as argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
E.g. if we send a List as an argument, it will still be a List when it reaches the function:
for x in teams:
players = [“rohit”, “kohli”, “bumrah”]
If we call a function without argument, it calls default value.
def our_function(state = “Karnataka”):
print(“I am from “ + state)
Add two asterisks ** before arguments if number of keyword arguments is not known.
print(“His language optional is “ + subject[“lang”])
my_optionals(math = “algebra”, lang = “French”)
Key, value pair arguments can be given to functions in python.
The order can be arbitrary.
def my_subjects(subject3, subject2, subject1):
print(“The first subject is “ + subject1)
my_subjects(subject1 = “English”, subject2 = “Kannada”, subject3 = “Hindi”)
When in doubt about number of arguments, add * before the argument.
print(“The World Cup teams are “ + teams)
world_cup(“India”, “South Africa”, “Australia”)
A function must have as many arguments as defined. If the number of arguments is less or more it will give an error.
def name_function(firstname, lastname):
print(firstname + ” “ + lastname)
Arguments are passed inside parentheses.
print(firstname + “Raj”)
There can be as many arguments as required, separated by a comma(,).
print(“Hello enterprising people”)
In the above example, def is a keyword.
It is used to define a function.