1
# Let's talk about arguments and parameters. The terms arguments and parameters are same information passed to function.
# There is differnce between both of them. A parameter is variables in function definition i.e. when function is defined.
# An argument is variable sent when the function is called.
def newfunction(num1,num2): # The variables num1 and num2 are parameters here in function definition.
value = 2*(num1+num2)
return value
n1 = 100
n2 = 200
ans = newfunction(n1,n2) # Here variables n1 and n2 are arguments.
print(ans)
def simpleinterest(p,t,r=10): # non-default arguments always come before default arguments.
si=(p*r*t)/100
return si
def total(p,si):
return p+si
principle=int(input("Enter principle"))
rate=int(input("Enter rate"))
time=int(input("Enter time"))
si= simpleinterest(principle,time,rate) # Teacher make both cases once pass value with rate and once without rate that means remove rate.
# si= simpleinterest(principle,time) # Here rate will always be considered 10
print(si)
amount=total(principle,si)
print(amount)
COMPOUND INTEREST FORMULA
2
# Function definition cannot be empty but just in case you have made the function but don't want define someting in it for now.
# You can use 'pass' keyword.
def checkfunction():
pass
3
# We have passed different data types inside function so let's pass list as argument.
#
def studentnames(names):
for x in names:
print(x)
names=['Ethan','Ali','Kenneth','Keyza']
studentnames(names)
4
# As you have seen that in each program we have to makesure the order of parameters and arguments.
# There is a way that we don't have to care about the order to pass values and it is called as Keyword Arguments.
# Let's say someone is having 4 students but he wants to pass value to function randomly but access the name of fourth student.
# In Keyword Arguments the variable name of parameters and arguments is same.
def students(stu3, stu2, stu4, stu1 ): # Here the names are passed randomly but the name of right student will be printed.
print("The youngest child is " + stu4)
students(stu1 = "Emily", stu2 = "Tobias", stu3 = "Tantowi", stu4="Devin")
5
# While filling forms you must have seen sometimes some value is written by default until you change it.
def nameOfCountry(country="Singapore"): # We have defined India as our default country.
print("I am from", country)
nameOfCountry("USA")
nameOfCountry("Indonesia")
nameOfCountry() # Here we have not defined the name of country so it will take singapore.
Arbritary Arguments
# Now we will learn about Arbitrary Arguments.
# If you do not know how many arguments that will be passed into your function,
# add a * before the parameter name in the function definition.
# This way the function will receive a tuple of arguments, and can access the items accordingly.
def names(*students): # Because we don't know how many arguments are passed or we don't want to write all of them.
# We can take a variable with * in starting then find the arguments just like tuple.
# It is immutable can't be changed. We can't change value in students.
print("The second student is", students[1])
names("Robert","Samuel","Omar","John")
Advanced