Python Function
Python Function
Output:
Hello world
Types of Arguments
1. Default arguments
Functions can be defined with default arguments. If the
values for the arguments are not supplied when the
function is called, the default argument is used.
def my_function(country='India'):
print("I am from:", country)
my_function("Australia")
my_function("USA")
my_function()
output
I am from: Australia
I am from: USA
I am from: India
2. Arbitrary arguments
Arbitrary arguments are used when you want the function to
take an unlimited number of arguments. When you add an
asterisk ( * ), it will receive a tuple of arguments.
def my_function(*args):
print(args)
my_function(2,6,9,3,7)
Output
(2,6,9,3,7)
3. Keyword arguments
You can pass the arguments in a non-positional
manner using keyword arguments. Let’s look at an
example to understand this:
The Return Statement in Python Functions
To return a value from a function, we use a return
statement. It “returns” the result to the caller.
def test(n1,n2):
sum=n1+n2
return sum
def count(L1):
even=0
odd=0
for i in L1:
if i%2==0:
even=even+1
else:
odd=odd+1
return even, odd
n=int(input("How many numbers U want to check?"))
L1=[]
for i in range(n):
L1.append(int(input("Enter the numbers:")))
even, odd=count(L1)
print("Total even numbers =",even)
print("Total odd numbers=",odd)