Functions in Python | Starting with python
This post is all about getting started with function in python how we declare a function and how it is similar or different from other languages like C, C++, and JAVA.
Function in python is almost the same as other languages but the difference is that it is started with the keyword def before function followed by a round parenthesis and due to the fact that python is space sensitive all the statements under the same function should be indented. otherwise, it will show error or give the wrong value.
Note:- The function should be declared before it is called otherwise it will show a message there is no function named as called function. e.g. is given below.
A function defined earlier before it is called
def fun(): #function definition print("This function is defined before it is called ",end="\n") print("and all the statement under same function is indented",end="\n") print("so that function could understand it's suit of block") a=10; b=45; print(a+b) fun() #function call
There are five types of function can be used in python
1.Function with no arguments and no return value.
2.Function with arguments and no return value.
3. Function with no argument but have a return value.
4.Function with argument and with a return value.
1.Function with no arguments and no return value.
Code:
def fun():Output :
print("this is function with no argument and no return value")
print("this programme shows how function with no argument \n")
print("and no return value works")
fun()
2.Function with arguments and no return value.
Code:
def fun(a,b): #function with argumentOutput :
print("Value of a: {}".format(a))
print("value of b : {}".format(b))
a=input("Enter value of a: ") #taking input from user
b=input("Enter value of b: ") #taking input from user
fun(a,b) #passing argument to user
3. Function with no argument but have a return value.
Code:
def fun():
string="Hello there i am returned value"
return string
a=10;
b=20
print(a+b)
return_value=fun()
print("this is return value {}".format(return_value))
Output:
4.Function with argument and with a return value.
Code:
def mul(a,b):
product=int(a*b)
return product
a=int(input("Enter value of a: "))
b=int(input("Enter value of b: "))
print("Multiplication of {} and {} is {}".format(a,b,mul(a,b)))
Output:
thank you all for reading this post, go check out other posts!
if you face any problem do comment below or Contact us.
0 Comments