Custom Functions in Python

Python Functions

Function is a group of statements that are created to perform a specific task. Function breaks the programs into smaller units. Print() is also a function which is defined in the python languages. These functions are called built in functions. And the functions that are defined and created by user is known as user defined function.

Syantax:

Def function_name(parameters)

    Statements

 

  1. Def keyword is used to start the function unit.
  2. Function name is given by the user. it is uniquely identify.
  3. In these brackets () the parameters are defined.
  4. : To end the function unit.
  5. Documentation string is optional part which is used to defined what the function does.
  6. Return to return the function value.

 

Example:

def wish( str ):

    “good morning”

    Print str

    Return;

Function call: In function definition we only specifies the name and parameters of the function if we want to call the function means want to execute the function than we have to call the function.

Syntax:

Def function_name(parameters)

    Statements

function_name( parameters value)                                      calling the function

 

Example:

def wish( str ):

    Print str

    return;

wish( “ good morning ” )

 

Function without arguments:

Syntax:

def function_name():

    statements

Example:

def wish( ):

    Print(“ good morning “)

wish()

 

Return statement:

Syntax:

Def function_name(parameters)

    Statements

    Return expression

function_name( parameters value)                                      

 

Example:

def add( a, b ):

    return ( a + b )

print( “Sum of two variable:” , add(10 , 5))

 

output: Sum of two variable: 15

 

Default argument values: in this function we can default set the value of the parameters, So that we have an option to declare or not to declare the value of the parameters.

Example:

def add( a, b=8 ):

    return ( a + b )

print( “Sum of two variables:” , add(10))

print( “Sum of two variables:” , add(10 , 5))

 

Output:

Sum of two variables: 18

Sum of two variables: 15

 

Keyword Parameters: it is another way to call the function.

Example:

def sum (a, b, c=10, d=20):

   return a + b + c + d

 

print(sum( 5, 15 ) )

print(sum(0, 1,d=10 ))

Output:

50

21

PYTHON RECURSION

Recursion is the process of defining something for itself. Let’s take an example to understand the recursion.

Example:

Factorial of 6 (6!). It means (1*2*3*4*5*6)

def fact(x):

if x == 1;

return 1

else:

return( x * fact(x-1))

a= 6

print(“factorial of number :”, fact( a))

 

Output and explanation:

Factorial of number: 120

Explanation:

 

Fact( 6 )

6  * Fact( 5 )

6 * 5 * fact( 4 )

6 * 5 * 4 * fact( 3 )

6 * 5 * 4 * 3 * fact( 2 )

6 * 5 * 4 * 3 * 2 * fact( 1 )

6 * 5 * 4 * 3 * 2 * 1

6 * 5 * 4 * 3 * 2

6 * 5 * 4 * 6

6 * 5 * 24

6 * 120

720

 

Advantages of recursion:

  1. It performs the complex calculation in easy way. It broke the code
  2. Sequence related problems are easier with recursion.
  3. It is clean and understandable.

 

Disadvantages of recursion:

  1. It takes more memories and time.
  2. Code of recursion is hard to understand
  3. Recursion is hard to debug.

 

Anonymous function

The functions which are not declare by using the def keywords are known as anonymous functions. They are declared by using the lambda keyword.

Lambda forms can take any number of arguments but return just one value in the form of an expression.

Lambda functions do not have the commands and multiple expressions

Lambda function needs the expression so they cannot be call by print function

They have their own local namespace.

Syntax:

Lambda[arg1 [,arg2,…… argn] ]:expression

Example:

Sum = lambda arg1, arg2: arg1 + arg2;

Print “total of arguments: “ , sum(10, 15 )

Print “total of arguments: “ , sum(20, 15 )

Output:

total of arguments: 25

total of arguments: 35

 

 

 

 

 

Share this post