Page 104 - Computer - 8
P. 104

For example, consider the code for a function given below:
                 def area_square(side):
                        area = side * side
                        return area
          Here, the name of the function is area_square. It accepts one argument – side. It calculates the area of
          the square. The value of area is returned by the return statement.

          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.
          These functions need not be defined and can be used in any program. 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 which can be used in
                        mathematical calculation.

             •  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. User-defined
          functions let a user add more functionalities to a program than provided by the built-in functions.


              102
   99   100   101   102   103   104   105   106   107   108   109