Python Function Basics Tutorial
Python Function:
A
function is a block of code to perform a specified task.
In
this context, there is a function call and there is a function definition.
When
a function is calling then the function definition is executing.
We
can pass data known as parameters(arguments) into a function.
At
the time of function call whatever we take arguments, is known as actual
arguments and at the time of function call, we take arguments is known as
formal arguments.
Here
actual arguments equals to the formal arguments.
A
function can return a value as result.
In
python a function is defined by using def
keyword.
Example(without arguments):
def
show(): #function definition
print('python means SILAN Technology')
show() #function call
output:
python
means SILAN Technology
Example(with arguments)
def
area(base,height): #function
definition
a=0.5*base*height
print("area of triangle is:",a)
area(4.5,5.6) #function call
output:
area of triangle is: 12.6
We can take any data types of argument to a function like string,
number, list, dictionary etc., and it will be treated as the same data type
inside the function.
For example, if we take a List as an argument, it will still be a
List when it reaches the function:
Example
def
fruits_detail(items):
for k in items:
print(k)
fruits
= ["apple", "banana","orange",
"cherry"]
fruits_detail(fruits)
output
apple
banana
orange
cherry
Return Values
A function returns a value explicitly by using return statement.
Example
def
calculate(x):
return 7* x
print(calculate(2))
print(calculate(3))
print(calculate(4))
output
14
21
28
Written
By:
Trilochan
Tarai
SILAN
Technology,BBSR
Comments
Post a Comment