Page 515 - ComputerScience_Class_11
P. 515

FOR ADVANCE LEARNERS: FUNCTIONS IN PYTHON





                 In Python, a function is a reusable block of code that performs a specific task. Instead of rewriting the same code
                 multiple times, a function allows us to write it once and use it as many times as needed. In this chapter, you will explore
                 how to efficiently use functions in Python, including various methods for passing information through arguments,
                 handling varying numbers of values and setting default values. Additionally, you will learn how functions return results
                 to the calling program, enhancing code modularity and flexibility.

                     FUNCTIONS IN PYTHON
                 A function in Python is a named block of code designed to carry out a specific task. Once defined, a function can be
                 called multiple times throughout a program, which helps avoid redundancy and enhances the program's organisation
                 and readability. The core idea behind functions is "define once, use many times." They allow large programs to be
                 broken into smaller, manageable parts, each handling a specific operation.
                 Python offers two main types of functions: built-in functions, which are readily available for use and user-defined
                 functions, which programmers can create to suit specific needs. Let's explore the types of functions in detail.

                 Built-in Functions
                 Built-in functions are pre-defined functions in Python, readily available for use without needing to be created. These
                 functions help programmers perform everyday tasks quickly and efficiently. They are designed to carry out specific
                 operations, such as displaying output, taking user input, determining the length of data or converting one data type to
                 another. By using built-in functions, programmers can save time and avoid reinventing the wheel.

                 Some commonly used built-in functions in Python include:
                 •  print(): Displays output on the screen. It is one of the most commonly used functions to show results or messages.

                 •  len(): Returns the length (i.e., the numbers of items) of an object such as a string, list or tuple.
                 •  type(): Returns the data type of a value. It helps determine whether a variable is an integer, string, list, etc. For
                   instance, type(42)returns <class 'int'>.
                 •  input(): Allows a program to take input from the user. The value entered by the user is always treated as a string.

                 •  int(): Converts a value into an integer. For example, int("10") converts the string "10" into the integer 10.
                 •  max() and min(): These functions return the largest and smallest values from a given set of values, respectively. For
                   example, max(3, 7, 2) returns 7, while min(3, 7, 2) returns 2.
                 •  range(): Generates a sequence of numbers. It is often used in loops to iterate over a specific range. For instance,
                   range(5) generates numbers from 0 to 4.

                 •  round(): Rounds a number to a specified number of decimal places. For example, round(3.14159, 2) returns
                   3.14.

                 •  sum(): Returns the sum of all items in an iterable (such as a list or tuple). For example, sum([1, 2, 3]) returns
                   6.
                 •  sorted(): Returns a sorted list from any iterable. For example, sorted([3, 1, 2]) returns [1, 2, 3].
                 Built-in functions are executed by writing the function name followed by parentheses (). If needed, values can be
                 passed inside the parentheses, which are called arguments.

                 Syntax of a built-in function:

                 function_name(arguments)




                                                                              For Advance Learners: Functions in Python  513
   510   511   512   513   514   515   516   517   518   519   520