Page 516 - ComputerScience_Class_11
P. 516

Where:
              •  function_name is the name of the built-in function you want to use.

              •   arguments are the values that you pass into the function. These values are used by the function to perform its task.
              Program 1: Write a program to find the length of a sentence using a built-in function.

                   Program 1.py
               File  Edit  Format    Run   Options   Window    Help


                sentence = input("Enter a sentence: ")
                length_of_sentence = len(sentence)
                print("The length of the sentence is:", length_of_sentence)




                   Output

                Enter a sentence: Hello, World!
                The length of the sentence is: 13




              User-Defined Functions
              A user-defined function is a function that you, as a programmer, create to perform a specific task. In Python, such
              functions are defined using the def keyword, followed by the function name and parentheses (). The code that makes
              up the body of the function is written inside a block, which must be properly indented to maintain the function's
              structure and ensure correct execution.

              A user-defined function involves two steps:
              1.  Defining a Function
              2.  Calling a Function


              Defining a Function
              Defining a function means writing the code that will be executed when the function is called.
              Syntax of defining a function:

              def function_name():
                  # statements
              Where:
              •  def is the keyword used to define a function.

              •  function_name is the name you assign to the function.
              •  statements are the lines of code inside the function that will run when the function is called.

              Calling a Function
              A function is executed only when it is called. To call a function, you write its name followed by parentheses ().

              Syntax for calling a function:
              function_name()






                  514  Touchpad Computer Science (Ver. 3.0)-XI
   511   512   513   514   515   516   517   518   519   520   521