Page 190 - Open Soource Technologies 304.indd
P. 190

Event Driven Programming



                                        A variable passed to a function procedure is normally passed by value. It can
                                        also be passed by reference and thereby possibly have its value changed by
                                        the function procedure. However, passing a variable by reference violates
                                        good design principles, since a function is intended to only create a single
                                        result and not cause any other changes.


                          Two examples of function procedure are as follows:
                          function FtoC(ByVal t As Double) As Double
                            ‘Convert Fahrenheit temperature to Celsius
                            Return (5/9) * (t –32)
                          End function
                          function FirstName(ByVal name As String) As String
                            ‘Extract the first name from a full name
                            Dim firstSpace As Integer
                            firstSpace = name.IndexOf(“ “)
                            Return name.Substring(0, firstSpace)
                          End function
                          The value of each of the preceding function is assigned by a statement of the form  return
                          expression. The variables t and name appearing in the preceding function are parameters. They
                          can be replaced with any variable of the same type without affecting the function definition. For
                          instance, the function FtoC could have been defined as
                          function FtoC(ByVal temp As Double) As Double
                            ‘Convert Fahrenheit temperature to Celsius
                            Return (5/9) * (temp –32)
                          End function

                                        1: The following program uses the function FtoC.


















                                 Object               Property             Setting

                                 frmConvert           Text                 Convert Fahrenheit to Celsius
                                 lblTempF             Text                 Temperature (Fahrenheit)
                                 txtTempF



                          184                    LOVELY PROFESSIONAL UNIVERSITY
   185   186   187   188   189   190   191   192   193   194   195