Function in Python

Function is a set of statements which is used to perform various task.Function is more helpful when you have too much line of code then you can make a function for any specific task.

Responsive image

Advantage of function

  • Function reduce the duplication of code.
  • The length of source program can be reduced by using function at appropriate places. This factor is particularly critical with micro computer where memory space is limited.
  • It is easy to locate and isolate a fault function are further investigate.
Function creation and Calling

def keyword are used to define the function.

Synatx of creation and calling
def function_name(arguments):
  #statements
  #statements
  return arguments
function_name(arguments)

Here function_name is the name of function it is start does't start with number or any special symbole like (! ,@ ,# ,$ ,^ ,&etc). In function we can pass zero or more arguments.return type may or may not be present.

Example
def multi(a,b):
  c=a*b
  return c
var=multi(6,5)
print(var)
Output
 30
Function with return value and with passing value

Python functions can return values, providing results for further computation or display. Passing values to functions allows for dynamic parameterization and tailored processing of data.

Example
#Here add is a function
def add(a,b):
   return x+y
val=add(5,6)
print("sum=",val)
Output
sum=11
Function with return value and without passing value

Functions in Python can return values, enabling the retrieval of computed results for further use. When not passing values, functions rely on predefined logic or external variables for processing.

Example
#Here add is a function
def add():
   a=5
   b=6
   return x+y
val=add(5,6)
print("sum=",val)
Output
sum=11
Function without return value and with passing value

Functions in Python can execute tasks without returning a value, streamlining procedural operations. Passing values to functions facilitates flexible and parameterized functionality within the program.


Example
#Here add is a function
def add(a,b):
   val=a+b
   print("sum=",val)
add(5,6)
Output
sum=11
Function without return value and without passing value

A function without a return value doesn't provide any output after execution, and if no value is passed, it operates without any input data.

Example
#Here add is a function
def add():
   a=5
   b=6
   val=a+b
   print("sum=",val)
add()
Output
sum=11
Function take default value

Functions can utilize default parameter values, allowing them to execute with predefined arguments when none are provided. This feature enhances flexibility and simplifies function calls by providing a default behavior.


Example
#Here add is a function
def add(a=5,b=6):
   print("a=",a,"b=",b)
add()
Output
a=5 b=6
Function take one default value one passed value

A function can take a default value if no value is passed, or it can use the passed value if one is provided.

Example
#Here add is a function
def add(a,b=6):
   print("a=",a,"b=",b)
add(a=5)
Output
a=5 b=6