Home

Exploring Procedures

 

Procedures and Conditional Statements

 

Using Conditions in Procedures

As seen in this and the previous lesson, a procedure is used to perform a specific assignment. When doing this, the procedure can encounter different situations, some of which would need to be checked for truthfulness or negation. This means that conditional statements can assist a procedure with its assignment, The conditions and comparisons primarily use the same rules we reviewed when studying conditional statements.

Suppose you write an ergonomic program that needs to check different things including answers from the user in order to proceed. These various assignments can be given to methods that would simply hand the results to the Main() function that can, in turn, send these results to other functions for further processing. Here is an example:

Module Module1

    Function GetPosition() As Char
        Dim Position As Char
        Dim Pos As String

        Do
            Console.Write("Are you sitting down now(y/n)? ")
            Pos = Console.ReadLine()
            Position = Char.Parse(Pos)

            If Position <> "y" And Position <> "Y" And _
                           Position <> "n" And Position <> "N" Then
                Console.WriteLine("Invalid Answer")
            End If
        Loop While Position <> "y" And Position <> "Y" And _
           Position <> "n" And Position <> "N"

        Return Position

    End Function
    Sub Main()
        Dim Position As String

        Position = GetPosition()

        If Position = "n" Or Position = "N" Then
   Console.WriteLine(vbCrLf & "Could you please sit down for the next exercise?")
        Else
            Console.WriteLine("Wonderful!!!" & vbCrLf)
        End If
    End Sub

End Module

Here is an example of running the program:

Are you sitting down now(y/n)? V
Invalid Answer

Are you sitting down now(y/n)? z
Invalid Answer

Are you sitting down now(y/n)? n

Could you please sit down for the next exercise?
 

Conditional Returns

 

A function is meant to return a value. Sometimes, it will perform some tasks whose results would lead to different results. A method can return only one value (we saw that, by passing arguments by reference, you can make a procedure return more than one value) but you can make it render a result depending on a particular behavior. If a procedure is requesting an answer from the user, since the user can provide different answers, you can treat each result differently.

Imagine you write the following function:

Module Module1

    Function GetPosition() As String
        Dim Position As Char

        Console.Write("Are you sitting down now(y/n)? ")
        Dim Pos As String = Console.ReadLine()
        Position = CChar(Pos)

        If Position = "y" Or Position = "Y" Then
            Return "Yes"
        ElseIf Position = "n" Or Position = "N" Then
            Return "No"
        End If
    End Function

    Sub Main()
        Dim Answer As String

        Answer = GetPosition()
        Console.WriteLine("Answer = {0}", Answer)
    End Sub

End Module

On paper, the function looks fine. If the user answers with y or Y, the function returns the string Yes. If the user answers with n or N, the function returns the string No. What if there is an answer that does not fit those we are expecting? The values that we have returned in the function conform only to the conditional statements and not to the function. Remember that in if(Condidion)Statement, the Statement executes only if the Condition is true. Here is what will happen. If the user answers y or Y, the function returns Yes and stops. If the user answers n or N, the function returns No, which also is a valid value. If the user enters another value (other than y, Y, n, or N), the execution of the function will not execute any of the returned statements. This means that the execution will reach the closing curly bracket without encountering a return value. This would mean to the compiler that you wrote a non-void method that is supposed to return a value, but by the end of the method, it didn't return a value. The solution is to provide a return value so that, if the execution reaches the end of the function, it would still return something. Here is a solution to the problem:

Module Module1

    Function GetPosition() As String
        Dim Position As Char

        Console.Write("Are you sitting down now(y/n)? ")
        Dim Pos As String = Console.ReadLine()
        Position = CChar(Pos)

        If Position = "y" Or Position = "Y" Then
            Return "Yes"
        ElseIf Position = "n" Or Position = "N" Then
            Return "No"
        End If

        Return "Invalid Answer"
    End Function

    Sub Main()
        Dim Answer As String

        Answer = GetPosition()
        Console.WriteLine("Answer = {0}", Answer)
    End Sub

End Module

Here is an example of running the program:

Are you sitting down now(y/n)? g

Answer = Invalid Answer

Exercises

  1. Create a program that provides an overloaded function with two versions that calculate the moment of inertia of a triangle depending on the considered axis. Write one version of the functions for the first formula and the other version for the second formula:
     
  2.  
 

 

 

 
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next