Python Functions

Functions are used to create our program to more manageable parts, we can reuse the code hence they are called re-usable methods.

Syntax:
def function_name(parameters):
"""docstring"""
statement(s)

  1. Keyword def marks the start of function header.
  2. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python.
  3. Parameters (arguments) through which we pass values to a function. They are optional.
  4. A colon (:) to mark the end of function header.
  5. Optional documentation string (docstring) to describe what the function does.
  6. One or more valid python statements that make up the function body. Statements must have same indentation level (usually 4 spaces).
  7. An optional return statement to return a value from the function.

 

Program on functions

def put():
print("Wlecome to vision Computers......")

print("hello the following line is generated from function")
put()

find the sum of 2 numbers

def sum():
a=int(input("Etner a value"))
b=int(input("Etner b value"))
c=a+b
print("sum of 2nos ",c)

# main programe
sum();

find the sum of 2numbers by passing arguments

def sum(x,y):

c=x+y
print("sum of 2nos ",c)

# main programe
a=int(input("Etner a value"))
b=int(input("Etner b value"))
sum(a,b);

find the sum of 2numbers by passing  arguments and return value

def sum(x,y):

c=x+y
return c

# main program
a=int(input("Etner a value"))
b=int(input("Etner b value"))
c=sum(a,b);
print("Sum ",c)

find the  sum of 2numbers no arguments and return value

def sum():
a=int(input("Etner a value"))
b=int(input("Etner b value"))
c=a+b
return c

# main program

c=sum();
print("Sum ",c)

global statement :

it is used to declare a variable to access any where in the program. It is impossible to assign a value to a variable defined outside a function without the global statement.

Program on global statement

a=20
def put():
global a
print(a)
a=30

 

#main programe
put()
print(a)  # prints 30

Default value

You can specify default argument values for parameters by appending to the parameter name in the function definition the assignment operator (=) followed by the default value.

def sum(x=10,y=20):
print(x,y)

# main programe

sum(3,2)
sum(5)
sum()

program on keyword arguments with default value

def put(x,y=5,z=2):
print(x,y,z)

put(10,4)
put(z=23,x=12)
put(12,z=2)

VarArgs parameters

When we declare a starred parameter such as *param, then all the positional arguments from that point till the end are collected as a tuple called 'param'.

Similarly, when we declare a double-starred parameter such as **param, then all the keyword arguments from that point till the end are collected as a dictionary called 'param'.

Program on varargs

def total(a=5, *numbers, **phonebook):
print('a', a)

    #iterate through all the items in tuple
for single_item in numbers:
print('Serial No: ', single_item)

    #iterate through all the items in dictionary   
for name, phoneno in phonebook.items():
print(name,phoneno)

total(10,1,2,3,prasad=1123,komali=2231,balaram=1560)

 

 

 

 

For
More Explanation
&
Online Classes

Contact Us:
+919885348743