Defining Functions in python (Theory)
Functions in Python :
Definition : A function is a block of organised and reusable program code that perform a
single , specific and well defined task.
single , specific and well defined task.
(1)A function is consist of two main parts
1.Function Header
2.Function Body/statements
(2) Python allows its programmer to break a code into small small functions each of which can
be written more or less independently on other
be written more or less independently on other
Called functions and calling function :
Here funct(1) is "called" function
funct(2) is "calling" function
code of 1 function can be fully isolate from other as well
Need of function :
1.Dividing the program into separate well define function facilitates each function to
be written and tested separately .this simplifies the process of program development
2.Understanding ,coding and testing multiple separate function are far easier than doing
same for one huge function .
3.Code reuse is one of the most prominent reason to use function .
4.Inbulid Libraries function speed up the program
5.Length of code get reduce
Syntax :
Till now we already deal with some inbuild libraries functions like len(),sqrt().
Besides using built in function user can also define there own functions ,such functions
are called as user define function .As a Python programmer you can write any number of functions in your program ,However to define a function you must have to keep following things in your mind .
1.function start with keyword " def "
2.this keyword is followed by the function name and parentheses/Brackets ().
3.function name must be unique
4. After the parentheses : colon must be placed
5.Arguments/parameters must be pass through parentheses is required
6.while writing a function indentation should be maintained properly.
7.Syntax :
Function calling :
syntax:
function name (parameters if exist )
NOTE:
List of variable used in function call is known as actual parameter list , actual parameter
list may be variables name , expression or constants .
Key Points :
while calling a function
1.The function name and the number of argument in function call must be same
2.parameter's list must be separated by commas " ,"
passing expression as an argument :
Argument can be pass as an expression to the called function ,in such case
argument i.e. expression is first evaluated then converted into the type of formal parameter
and thenthe body of a function gets evaluated .
e.g.
def equation(i): # defining a function
print(i) #body
equation(2+3*4) #calling a function
output = 14
The Return Statement :
In all our function written above ,no where we have used the return statement .But
you will be surprised to know that every function has an implicit return statement as a last
statement in any function body .This implicit return statement return nothing to its caller ,
It's said to return none.
Syntax:
return (expression)
def cube (x):
return (x*x*x)
n=10
result =cube(n)
print("Cube of ",n,"=",result)
OUTPUT= Cube of 10 = 1000
Example :
def show():
print("Hello")
return()
print("This line will never be display")
show()
print("Good morning ")
output :
Hello
Good morning
This is all about basics of function in Python , and in next article we will discuss about
some more important functions and their algorithm
I hope this article on functions is helpful for you.
Thank u 😊
some more important functions and their algorithm
I hope this article on functions is helpful for you.
Thank u 😊