Professional Documents
Culture Documents
A function is a group of statements that exists within a program for the purpose of performing a
specific task.
Advantages of functions:
1. Easier program handling.
2. Reduced line of codes.
3. Easy updating.
Types:
1. Built-in Functions
2. Modules
3. User-defined Functions
BUILT-IN FUNCTIONS
> They are available in the standard library and do not require import.
> Provide efficiency and structure.
> Faster and more powerful programming.
I. Type conversion functions:
i) int():
ii) str():
iii) float():
II. input() function:
Enable accepting input in desired form from the user without evaluating it's value.
III. eval() function:
IV. max() and min() functions:
V. abs() function:
Syntax: abs(argument)
VI. type() function:
Syntax: type(variable)
VII. len() function:
Syntax: len(object)
VIII. round() function:
Returns the number rounded up-to the specified decimal places.
All trailing decimal places beyond the . are converted to zero.
Syntax: round(n,p)
IX. range() function:
Syntax: range(n)
MODULES
For one-
ii) Create space where modules definition and variable are created.
#import statement:
The import statement creates an access to the functions/modules in a program. This is done by
specifying the allotted name, separated by a dot (this dot is known as dot notation).
1. Math module
Syntax:
x=10.0
math.ceil(x)
>>> 10
ceil(x) floor(x) pow(x,y) fabs() sqrt(x)
Smallest integer that is Largest integer that is less Value of xy in float Absolute positive Square root of x.
greater than or equal to x. than or equal to x. point value. value in float point. Condition: x>0
2. String module
i) random module
randrange() random() randint()
Generates an integer b/w it's lower and Generates a random number between 0 Generates a number in the inclusive
upper argument.
and 1, i.e., it's range is (0.1, 1.0). range (a,b) such that a<=N<=b
Default lower: 0
USER-DEFINED FUNCTIONS
Python allows user to write their own function(s) which can be combined into module.
Syntax:
Statement(s)
1. Keyword 'def' marks the start of function header.
2. A function name to uniquely identify it.
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. Valid Python statements of the same indentation level make up the function body.
6. An optional return statement to return a value from the function.
7. Function must be called/invoked to execute its code.
Void Function:
• A function that doesn't contain a return statement.
• e.g.: def display():
print("This is an example of a void function.")
Importance of Indentation:
Python functions don't have any explicit beginning or end (like curly brackets) to indicate the start
or end for function, they have to rely on indentation for the same.
• They indicate the beginning and end of a user-defined function.
• Returns "expected an indented block" error.
• The indentation should remain the same for the entire body of the function.
return command:
Syntax:
return<value>
• It is used to end the execution of a function. termed as
• Specifies the to be returned value to the calling function. "fruitful function"
Returning multiples:
i) as variables:
▪
• Returning without variable:
PARAMETERS
Parameters are the value(s) provided in the parenthesis when we write function header.
1. These are the values required by function to work.
2. For more than one values are listed in parameter list separated by comma.
3. Example:
PASSING OBJECTS AS ARGUMENTS
ARRAYS/LISTS STRINGS TUPLE DICTIONARY
1. Are mutable and can be Are immutable and used as Are immutable and used as Are mutable and can be
passed as variables. pass by value. pass by value. passed as variables.
2. Can be edited and viewed. Can only be viewed. Can only be viewed. Can be edited and viewed.
Extra Lists are implemented as To modify a new string is To modify convert to list, -
arrays. created and concatenated. modification is valid inside
Lists>>Arrays
only.
(as lists are homogenous.)
• Outside the function, any modifications and calculations get deleted automatically.
SCOPE OF VARIABLES
Scope of variables is that part of the program which holds the current set of parameters and their values.