Page 74 - Code & Click - 8
P. 74
TYPES OF FUNCTIONS IN PYTHON
Python functions can be categorised as Built-in functions and User-defined functions.
Built-in Functions
The Python Built-in functions are defined as the functions whose functionality is predefined in
Python. Some commonly used built-in functions in Python are:
• int( ): It is used to convert a value stored as string to integer value.
For example,
int(3434.60) will give the result 34.
int(“123”) will convert the string to numeric value 123.
• str( ): It converts any type of value to a string of characters.
For example,
str(123) will convert the numeric value 123 to a string of 3 characters – “123”, which
cannot be used in mathematical calculations.
• print( ): It displays the argument value as output on the screen
For example,
print(“I Love Python”) will display the message I Love Python on the screen.
print(12 + 9) will display the result of the calculation, i.e., 21.
• input( ): It accepts a value from the user through the keyboard and stores it in a variable.
For example,
X = input(“Enter the value”)
The above command will show a message to the user “Enter the value” and the value
typed by the user gets stored as a string in variable x.
• range( ): This function is used to define a series of number with a specified gap.
For example,
range(1, 10, 2) will generate a sequence of number from 1 to 9 with a gap of number 2,
i.e., 1, 3, 5, 7, 9.
User-defined Functions
A function created by the user for performing a specific task is called a User-defined function. Once
a user has created a function, it can be called in the same way as a built-in function.
CREATING A FUNCTION
Python provides the def keyword to create a user-defined function. The syntax to create a function is:
def <function name>(arguments list):
body of function
return <expression>
Here, def is the keyword which tells the interpreter that a function is being defined.
function name is the name of the user-defined function.
arguments list contains all the values given to the function as input. The arguments can be a
value, a variable, or an expression. In case of more than one arguments passed to the function, they
are separated by commas ( , ).
body of function contains all the executable statements required by the function.
return statement specifies the value returned by the function as output.
72