Python Functions

What is Functions?

  1. Function is nothing but collection of statements to perform a specific task
  2. By using functions we divided large programs as multiple modules
  3. It is help to debug the code easily
  4. Modification of code became very easy
  5. Depend of statements will be decrease
  6. Repetation of same set of statement code will be avoided

Syntax:

 def function_name(arg1,arg2……..argn):

            ”’doc string”’

            statement 1


            statement 2

            ……………..

            ……………..


            ……………..

            statement n

            return statement

A function contains two parts , header and body

Header:

  • Header is start with ‘def’ keyword and followed by nae of the fucntion,arguments and finally haveing the ‘:’ to tell the compiler function defination has been completed
  • def is a reserved keyword,funcion name should be a any valid identifier,here argument is optional,it is depend on the function implementation.
  • Here docstring also optional

Body:

  • Body is tell to the function what should does
  • return statement is an optional,if caller is required to get any return value from the defination then we can use the return statement

1. User defined function without arguments:

Example:

Input:

Enter a number:3

Enter a number:2

COMPILE & RUN

Output:

2. User defined function with arguments:

Example:

Input:

Enter a number:3

Enter a number:2

COMPILE & RUN

Output:

Return Statement:

            The return statement is used to quite the fucntion and recah back to where it was called

syntax:

            return statement

How to use return single value:

Example:

Input:

Enter a number:3

Enter a number:2

COMPILE & RUN

Output:

Explanation:

1. Defined a fucntion with name of sum with arguments

2. read the values a,b

3. called that function with a,b values

4. once invoked the function

5. it will perform addition between a,b and it will return the result to caller

6. The returned value we are storing res variable

7. After print res it will print the result

How to use return multiple values:

Example:

 

Input:

Enter a number:3

Enter a number:2

COMPILE & RUN

Output:

Scope and life time of variable:

Scope:

scope of the variable is nothing but part of the program where it can be accessed

Life Time of the Variable:

Life time of the variable is nothing but period of the variable through out it exists

There is two types of variables are available.They are

1. Local Variable

2. Global Variable

1. Local Variables:

            The local variable is nothing but whichever the variable we can create inside the function defination we can call it as Local variables.It can be accessble and life time of the variable within the fucntion only

2. Global Variables:

            The global variable is nothing but whichever the variable we can create ouside the function defination we can call it as Global variables.It can be accessble and life time of the variable end of the program

Example 1:

COMPILE & RUN

Output:

Example 2:

COMPILE & RUN

Output:

Traceback (most recent call last):

  File “fun5.py”, line 8, in <module>

    print (“Outside Fun Local variable is:”,loc_var)

NameError: name ‘loc_var’ is not defined  

Note: If we observe above example ,i am trying to access the local variable outside of the function,after execute got is not defined ,because of it is a local variable .

Example 3:

COMPILE & RUN

Output:

Default Argument or Default Parameters:

Default arguments is nothing but when ever we are defining the fucntion at the time only we can intialize the variable to that function defination and whenever we  invoke the function without any arguments that time it will take from the already assigned values to arguments inside the defination

Syntax:

def fun_name(arg=value1,arg2=value2……..argn=valuen):

Example:

COMPILE & RUN

Output:

 

Share this website !!!
Facebook Twitter Linkedin