



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Definition: Functions are the subprograms that perform specific task. Functions are the small modules. Types of Functions: There are three types of functions in python: Library Functions: These functions are already built in the python library. Functions defined in modules: These functions defined in particular modules. When you want to use these functions in program, you have to import the corresponding module of that function. User Defined Functions: The functions those are defined by the user are called user defined functions. Library Functions in Python: These functions are already built in the library of python. For example: type( ), len( ), input( ) etc. Functions defined in modules: Functions of math module: To work with the functions of math module, we must import math module in program. import math
Function in random module: randint( )- function generates the random integer values including start and end values. Syntax: randint(start, end) It has two parameters. Both parameters must have integer values. Example: import random n=random.randint(3,7) *The value of n will be 3 to 7. User defined functions: The syntax to define a function is: def function-name ( parameters) : #statement(s) Keyword def marks the start of function header. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python. Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of function header. One or more valid python statements that make up the function body. Statements must have same indentation level.
Syntax: function-name(parameter) Example: ADD(10,20) OUTPUT: Sum = 30. The return statement: The return statement is used to exit a function and go back to the place from where it was called. There are two types of functions according to return statement: a. Function returning some value b. Function not returning any value a. Function returning some value : Syntax: return expression/value Example-1: Function returning one value def my_function(x): return 5 * x Example-2 Function returning multiple values: def sum(a,b,c): return a+5, b+4, c+ S=sum(2,3,4) # S will store the returned values as a tuple print(S) OUTPUT: (7, 7, 11) Example-3: Storing the returned values separately: def sum(a,b,c): return a+5, b+4, c+ s1, s2, s3=sum(2, 3, 4) # storing the values separately
print(s1, s2, s3) OUTPUT: 7 7 11 b. Function not returning any value : The function that performs some operations but does not return any value, called void function. def message(): print("Hello") m=message() print(m) OUTPUT: Hello None Scope and Lifetime of variables: Scope of a variable is the portion of a program where the variable is recognized. Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope. There are two types of scope for variables: i) Local Scope ii) Global Scope Local Scope: Variable used inside the function. It cannot be accessed outside the function. In this scope, the lifetime of variables inside a function is as long as the function executes. They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls. Global Scope: Variable can be accessed outside the function. In this scope, Lifetime of a variable is the period throughout which the variable exits in the memory.
return (prt)/ Q2. Consider the following function headers. Identify the correct statement: -