0% found this document useful (0 votes)
4 views16 pages

Function in Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Function in Python

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

.

15.2.2022 Class work


Read and write once in a paper and submit to representative within this
hour
1..What is a function?
Functions are "self-contained" modules of code that accomplish a specific task.
Functions usually "take in" data, process it, and "return" a result. Once a
function is written, it can be used over and over and over again. Functions can
be "called" from the inside of other functions.

2.Give the differences between recursion and iteration

3.What are advantages and disadvantages of recursion?

Example: Iterative algorithm for factorial of a number


Example: Recursive algorithm for factorial of number

4.What is the need for function and list types of function and explain?

Need For Function:

 When the program is too complex and large they are divided into parts.
Each part is separately coded and combined into single program. Each
subprogram is called as function.
 Debugging, Testing and maintenance becomes easy when the program is
divided into subprograms.
 Functions are used to avoid rewriting same code again and again in a
program.
 Function provides code re-usability
 The length of the program is reduced.

Types of function:

Functions can be classified into two categories:


i) user defined function
ii) Built in function

i) Built in functions

 Built in functions are the functions that are already created and stored in
python.

These built in functions are always available for usage and accessed by a
programmer. It cannot be modified.

ii)User Defined Functions:


 User defined functions are the functions that programmers create for
their requirement and use.
 These functions can then be combined to form module which can be
used in other programs by importing them.
 Advantages of user defined functions:
 Programmers working on large project can divide the workload by
making different functions.
 If repeated code occurs in a program, function can be used to include
those codes and execute when needed by calling that function.

5.How to define a function ? explain with syntax.

Function definition: (Sub program)

 def keyword is used to define a function.


 Give the function name after def keyword followed by parentheses in
which arguments are given.
 End with colon (:)
 Inside the function add the program statements to be executed
 End with or without return statement

Syntax:
def fun_name(Parameter1,Parameter2…Parameter n):
statement1
statement2…
statement n
return[expression]

Example:
def my_add(a,b):
c=a+b
return c
6.What you meant by function prototype

Function Prototypes:

i. Function without arguments and without return type


ii. Function with arguments and without return type
iii. Function without arguments and with return type
iv. Function with arguments and with return type

i) Function without arguments and without return type

o In this type no argument is passed through the function call and no


output is return to main function
o The sub function will read the input values perform the operation and
print the result in the same block

ii) Function with arguments and without return type

o Arguments are passed through the function call but output is not return
to the main function

iii) Function without arguments and with return type

o In this type no argument is passed through the function call but output
is return to the main function.

iv) Function with arguments and with return type

In this type arguments are passed through the function call and output is
return to the main function
Function definition: (Sub program)

 def keyword is used to define a function.


 Give the function name after def keyword followed by parentheses in
which arguments are given.
 End with colon (:)
 Inside the function add the program statements to be executed
 End with or without return statement

Syntax:
def fun_name(Parameter1,Parameter2…Parameter n):
statement1
statement2…
statement n
return[expression]

Example:
def my_add(a,b):
c=a+b
return c

Function Calling: (Main Function)

 Once we have defined a function, we can call it from another function,


program or even the Python prompt.
 To call a function we simply type the function name with appropriate
arguments.
Example:
x=5
y=4
my_add(x,y)
Flow of Execution:

 The order in which statements are executed is called the flow of


execution
 Execution always begins at the first statement of the program.
 Statements are executed one at a time, in order, from top to bottom.
 Function definitions do not alter the flow of execution of the program,
but remember that statements inside the function are not executed until
the function is called.
 Function calls are like a bypass in the flow of execution. Instead of going
to thenext statement, the flow jumps to the first line of the called function,
executes all the statements there, and then comes back to pick up where it
left off.

Note: When you read a program, don’t read from top to bottom. Instead,
follow the flow of execution. This means that you will read
the def statements as you are scanning from top to bottom, but you should
skip the statements of the function definition until you reach a point where
that function is called.

Function Prototypes:

i. Function without arguments and without return type


ii. Function with arguments and without return type
iii. Function without arguments and with return type
iv. Function with arguments and with return type

i) Function without arguments and without return type

o In this type no argument is passed through the function call and no


output is return to main function
o The sub function will read the input values perform the operation and
print the result in the same block
ii) Function with arguments and without return type

o Arguments are passed through the function call but output is not return
to the main function

iii) Function without arguments and with return type

o In this type no argument is passed through the function call but output
is return to the main function.

iv) Function with arguments and with return type

In this type arguments are passed through the function call and output is
return to the main function

Without Return Type

Without argument
def add():
a=int(input("enter a"))
b=int(input("enter b"))
c=a+b
print(c)
add()

OUTPUT:
enter a 5
enter b 10
15

With argument
def add(a,b):
c=a+b
print(c)
a=int(input("enter a"))
b=int(input("enter b"))
add(a,b)

OUTPUT:
enter a 5
enter b 10
15

With return type

Without argument
def add():
a=int(input("enter a"))
b=int(input("enter b"))
c=a+b
return c
c=add()
print(c)

OUTPUT:
enter a 5
enter b 10
15

With argument
def add(a,b):
c=a+b
return c
a=int(input("enter a"))
b=int(input("enter b"))
c=add(a,b)
print(c)

OUTPUT:
enter a 5
enter b 10
15

Parameters And Arguments:

Parameters:
 Parameters are the value(s) provided in the parenthesis when we
write function header.
 These are the values required by function to work.
 If there is more than one value required, all of them will be listed in
parameter list separated by comma.
 Example: def my_add(a,b):

Arguments :
 Arguments are the value(s) provided in function call/invoke
statement.
 List of arguments should be supplied in same way as parameters are
listed.
 Bounding of parameters to arguments is done 1:1, and so there
should be same number and type of arguments as mentioned in parameter
list.
 Example: my_add(x,y)

RETURN STATEMENT:

 The return statement is used to exit a function and go back to the


place from where it was called.
 If the return statement has no arguments, then it will not return any
values. But exits from function.

Syntax:
return[expression]

Example:
def my_add(a,b):
c=a+b
return c
x=5
y=4

print(my_add(x,y))

Output:
9

ARGUMENTS TYPES:
1. Required Arguments
2. Keyword Arguments
3. Default Arguments
4. Variable length Arguments

1. Required Arguments:
The number of arguments in the function call should match exactly with
the function definition.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details("george",56)
Output:
Name: george
Age 56

2. Keyword Arguments:
Python interpreter is able to use the keywords provided to match the
values with parameters even though if they are arranged in out of order.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details(age=56,name="george")
Output:
Name: george
Age 56
3. Default Arguments:
Assumes a default value if a value is not provided in the function call for
that argument.
def my_details( name, age=40 ):
print("Name: ", name)
print("Age ", age)
return
my_details(name="george")
Output:
Name: george
Age 40

4. Variable length Arguments

If we want to specify more arguments than specified while defining the


function, variable length arguments are used. It is denoted by *
symbol before parameter.
def my_details(*name ):
print(*name)
my_details("rajan","rahul","micheal",
ärjun")

Output:
rajan rahul micheal ärjun

MODULES:
 A module is a file containing Python definitions ,functions,
statements and instructions.
 Standard library of Python is extended as modules.
 To use these modules in a program, programmer needs to import
the module.
 Once we import a module, we can reference or use to any of its functions
or variables in our code.
o There is large number of standard modules also available in python.
o Standard modules can be imported the same way as we import our user-
defined modules.
o Every module contains many function.
o To access one of the function , you have to specify the name of the module
and the name of the function separated by dot . This format is called
dot notation.

Syntax:
import module_name
module_name.function_name(variable)

Importing Builtin Module:


import math
x=math.sqrt(25)
print(x)

Importing User Defined Module:


import cal
x=cal.add(5,4)
print(x)
Built-in python modules are,

1.math – mathematical functions:


some of the functions in math module is,
math.ceil(x) - Return the ceiling of x, the smallest integer greater than or
equal to x
math.floor(x) - Return the floor of x, the largest integer less than or equal to
x.
math.factorial(x) -Return x factorial.
math.gcd(x,y)- Return the greatest common divisor of the integers a and b

math.sqrt(x)- Return the square root of x


math.log(x)- return the natural logarithm of x math.log10(x) – returns the
base-10 logarithms math.log2(x) - Return the base-2 logarithm of x.
math.sin(x) – returns sin of x radians
math.cos(x)- returns cosine of x radians
math.tan(x)-returns tangent of x radians
math.pi - The mathematical constant π = 3.141592 math.e – returns The
mathematical constant e = 2.718281

2. .random-Generate pseudo-random numbers


random.randrange(stop)
random.randrange(start, stop[, step])
random.uniform(a, b)
-Return a random floating point number

You might also like