Functions:
In the context of programming, a function is a named sequence of statements that performs a
computation. When a function is defined, name should be specified followed by the sequence of
statements. Later, the function can be called by name.
Built in functions:
Python provides a number of important built-in functions that can be used without needing to
provide the function definition. The creators of Python have written a set of functions to solve
common problems and included them in Python for programmers to use.
Ex of built in functions : max(), min (), len()
Python also provides built-in functions that convert values from one type to another.
Ex: int(3.99999) prints 3
Python has a math module that provides most of the familiar mathematical functions. Before using
the module, it has to be imported.
import math
print([Link](4))
O/p: 2
The random module provides functions that generates random numbers. The function random
returns a random float between 0.0 and 1.0.
Ex:
import random
for i in range(10):
x = [Link]()
print(x)
O/p:
0.11132867921152356
0.5950949227890241
0.04820265884996877
0.841003109276478
0.997914947094958
0.04842330803368111
0.7416295948208405
0.510535245390327
0.27447040171978143
0.028511805472785867
The function randint takes the parameters low and high, and returns an integer between low and
high.
Ex :
import random
[Link](5, 10)
O/p: 6
User defined functions:
A function definition specifies the name of a new function and the sequence of statements that
execute when the function is called. Once a function is defined, it can be reused over and over
throughout the program.
Ex:
def print_lyrics():
print("Wheels on the bus go ")
print(“Round and round all day long”)
def is a keyword that indicates that this is a function definition. The name of the function is
print_lyrics. The rules for function names are the same as for variable names: letters, numbers and
some punctuation marks are legal, but the first character can’t be a number. A keyword cannot be
used as the name of a function. The empty parentheses after the name indicate that this function
doesn’t take any arguments. The first line of the function definition is called the header; the rest is
called the body. The header has to end with a colon and the body has to be indented. The body can
contain any number of statements.
Once the function is defined it can be called.
Ex :
print_lyrics()
print_lyrics()
print_lyrics()
This will print the lyrics 3 times.
Note: A function cannot be called before it is defined. An attempt to do so results in error.
Execution of a program always begins at the first statement of the program. Statements are
executed one at a time, in order from top to bottom. A function call is like a detour in the flow of
execution. Instead of going to the next statement, the flow jumps to the body of the function,
executes all the statements there, and then comes back to pick up where it left off.
Arguments:
Information can be passed into functions as arguments. Arguments are specified after the function
name, inside the parentheses. One can add as many arguments as needed. They have to be
separated with a comma. Arguments can be variables of any type. int, string, dictionaries, lists. The
following example has a function with one argument (fname). When the function is called, a first
name is passed along, which is used inside the function to print the full name:
O/p:
Return Values:
To let a function return a value, the return statement is used:
Ex:
O/p:
Why functions?
• Creating a new function gives an opportunity to name a group of statements, which makes a
program easier to read, understand, and debug.
• Functions can make a program smaller by eliminating repetitive code. Later, if a change is to be
made, it has to be done in only one place.
• Dividing a long program into functions helps in easier debugging.
• Well-designed functions are often useful for many programs. Once written and debugged, it can be
reused.