Debugging a Function

Introduction

We already know that a function is a section of code that solves one particular problem, and a function has a body delimited by curly brackets. These two characteristics provides some features to a function when it comes to debugging.

Practical LearningPractical Learning: Introducing Function Debugging

  1. Start Microsoft Visual Studio
  2. Create a new Console App named ObliqueTriangles2
  3. Change the document as follows:
    SAS ASA
    Oblique Triangles Oblique Triangles
    Known Values: 2 sides and the 1 angle between them
    Unknown Values: 2 angles and the 1 sides between them
    Known Values: 2 angles and the 1 side joining them
    Unknown Values: 2 sides and the 1 angle between them
    AAS SSS
    Oblique Triangles Oblique Triangles
    Known Values: 2 angles and the 1 side opposite the first angle
    Unknown Values: 2 sides and the 1 angle oppiste the second side
    Known Values: All 3 sides
    Unknown Values: All 3 angles
    using static System.Console;
    
    void CalculateAAS()
    {
        double angle1 = 0.00, angle2 = 0.00, side1 = 0.00;
    
        WriteLine("AAS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  Known Values: 2 sides and the 1 angle between them");
        WriteLine("Sought Values: 2 angles and the 1 sides between them");
        WriteLine("----------------------------------------------------------------------");
    
        try
        {
            Write("AAS Angle 1: ");
            angle1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the lower left angle of the AAS shape.");
        }
    
        try
        {
            Write("AAS Angle 2: ");
            angle2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the lower right angle of the AAS shape.");
        }
    
        try
        {
            Write("AAS Side 1:  ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the right side of the AAS shape.");
        }
    
        // Here, we use the law of sines
        double angle3 = 180 - (angle1 + angle2);
        double side2 = side1 * Math.Sin(angle2 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
        double side3 = side1 * Math.Sin(angle3 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("AAS Angle 3:  " + angle3.ToString());
        WriteLine("AAS Side  2:  " + side2.ToString());
        WriteLine("AAS Side  3:  " + side3.ToString());
    }
    
    void CalculateASA()
    {
        double angle1 = 0.00, angle2 = 0.00, side1 = 0.00;
    
        WriteLine("ASA Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  2 angles and the 1 side joining them");
        WriteLine("Sought Values: 2 sides and the 1 angle between them");
        WriteLine("----------------------------------------------------------------------");
    
        try
        {
            Write("ASA Angle 1: ");
            angle1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known angles of the ASA shape.");
        }
    
        try
        {
            Write("ASA Side 1:  ");
            side1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the side that is between the angles whose values are about the ASA shape.");
        }
    
        try
        {
            Write("AAS Angle 2: ");
            angle2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the other known angle of the ASA oblique triangle.");
        }
    
        // Here, we use the law of sines
        double angle3 = 180 - (angle1 + angle2);
        double side2 = side1 * Math.Sin(angle2 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
        double side3 = side1 * Math.Sin(angle3 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("ASA Angle 3: {0}", angle3);
        WriteLine("ASA Side  2:  {0}", side3);
        WriteLine("ASA Side  3:  {0}", side2);
    }
    
    void CalculateSAS()
    {
        WriteLine("SAS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  2 sides and 1 angle");
        WriteLine("Sought Values: 1 unknown side and 2 unknown angles");
        WriteLine("----------------------------------------------------------------------");
    
        double side1 = 0.00, angle1 = 0.00, side2 = 0.00;
    
        WriteLine("Type the known values");
    
        try
        {
            Write("SAS Side  1: ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known sides of the SAS shape.");
        }
    
        try
        {
            Write("SAS Angle 1: ");
            angle1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the known angle of the SAS shape.");
        }
    
        try
        {
            Write("SAS Side  2: ");
            side2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the other known side of the SAS shape.");
        }
    
        // Here, we use the law of cosines
        double side3 = Math.Sqrt((side1 * side1) +
                                 (side2 * side2) -
                                 (2 * side1 * side2 * Math.Cos(angle1 * Math.PI / 180)));
        double angle2 = Math.Acos(((side3 * side3) +
                                   (side2 * side2) -
                                   (side1 * side1)) /
                                   (2 * side3 * side2)) * 180 / Math.PI;
        double angle3 = 180 - (angle1 + angle2);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("SAS Side  3: {0}", side3.ToString());
        WriteLine("SAS Angle 2: {0}", angle2.ToString());
        WriteLine("SAS Angle 3: {0}", angle3.ToString());
    }
    
    void CalculateSSS()
    {
        WriteLine("SSS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  All 3 sides");
        WriteLine("Sought Values: All 3 angles");
        WriteLine("----------------------------------------------------------------------");
    
        double side1 = 0.00, side2 = 0.00, side3 = 0.00;
    
        WriteLine("Type the known values");
    
        try
        {
            Write("SSS Side 1:  ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known sides of the SSS shape.");
        }
    
        try
        {
            Write("SSS Side 2:  ");
            side2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for another side of the SSS triangle.");
        }
    
        try
        {
            Write("SSS Side 3:  ");
            side3 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the remaining known side of the SSS oblique triangle.");
        }
    
        // Here, we use the law of cosines
        double angle1 = Math.Acos(((side3 * side3) +
                                   (side2 * side2) -
                                   (side1 * side1)) /
                                   (2 * side3 * side2)) * 180 / Math.PI;
        double angle2 = Math.Acos(((side3 * side3) +
                                   (side1 * side1) -
                                   (side2 * side2)) /
                                   (2 * side3 * side1)) * 180 / Math.PI;
        double angle3 = 180 - (angle1 + angle2);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine($"SSS Angle 1: {angle1}");
        WriteLine($"SSS Angle 2: {angle2}");
        WriteLine($"SSS Angle 3: {angle3}");
    }
    
    WriteLine("======================================================================");
    WriteLine("This application allows you to make calculations on oblique triangles");
    WriteLine("======================================================================");
    
    Title = "Oblique Triangles";
    
    WriteLine("Available shape options:");
    WriteLine("1 - AAS (Known: Angle - Angle - Side)");
    WriteLine("2 - ASA (Known: Angle - Side - Angle)");
    WriteLine("3 - SAS (Known: Side - Angle - Side)");
    WriteLine("4 - SSS (Known: Side - Side - Side)");
    WriteLine("Q - Quit");
    Write("What type of triangle do you want to process? ");
    string selection = ReadLine()!;
    
    switch (selection)
    {
        case null:
            break;
        case "4":
            CalculateSSS();
            break;
        case "3":
            CalculateSAS();
            break;
        default:
            break;
        case "1":
            CalculateAAS();
            break;
        case "2":
            CalculateASA();
            break;
    }
    
    WriteLine("======================================================================");

Stepping Into a Function

So far, that is in previous lessons, we saw various ways to monitor the variables of a document and their values. All the techniques we studied are also available for variables declared and/or used in a function. It should be a good idea to review the issues.

Practical LearningPractical Learning: Introducing Function Debugging

  1. To start debugging, on the main menu, click Debug -> Step Into.
    Notice that the debugging focus jumps to the first line of execution.
    Move the DOS window so you can see both your code and that window
  2. To continue debugging, on the main menu, click Debug -> Step Into; do that twelve times
  3. When the type of triangle is requested in the DOS window, type 4 and press Enter
  4. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice that the debugging focus has moved where the CalculateSSS() function is called:

    Debugging Functions

Debugging the Variables of a Function

In previous lessons, we saw that the Step Into operation monitors the variables in a document. We also saw that the Locals window shows all the variables declared in a document. When it comes to a function, when debugging a function, the Locals window allows you to monitor all the variables declared in the function that is currently receiving the debugging operation.

Practical LearningPractical Learning: Debugging the Variables of a Function

  1. To continue debugging, on the main menu of Microsoft Visual Studio, click Debug and click Step Into.
    Notice that the debugging focus has moved to the begining of the body of the CalculateSSS() function.
    Notice that the Locals window is showing the variables declared in the CalculateSSS() function:

    Debugging Functions

    Click the Autos tab. Notice that it is empty because the variables of the function have not yet been accessed.
    Click the Locals tab to return to its window
  2. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice the content of the Locals window where the variables are still showing default values
  3. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twelve times.
    Notice where the debugging focus is currently
  4. In the DOS window where SSS Side 1 is requested, type 4.5 and press Enter.
    Check the values in the Locals window.
    Position the mouse on each variable (side1, side2, and side3) in the CalculateSSS() function
  5. To continue debugging, on the Debug toolbar, click the Step Into button Step Into four times
  6. When the value of the AAS Side 2 is requested in the DOS window, type 3.5 and press Enter.
    Check the values in the Locals window.
    Position the mouse on each variable (side1, side2, and side3) in the CalculateSSS() function
  7. To continue debugging, on the Debug toolbar, click the Step Into button Step Into four times
  8. When the value of the AAS Side 3 is requested in the DOS window, type 2.9 and press Enter.
    Check the values in the Locals window.
    Position the mouse on each variable (side1, side2, and side3) in the CalculateSSS() function
  9. To continue debugging, on the Debug toolbar, click the Step Into button Step Into ten times. Every time you click, check the values in the Locals and the Autos window:

    Debugging Functions

  10. To continue debugging, on the Debug toolbar, click the Step Into button Step Into four times
    ======================================================================
    This application allows you to make calculations on oblique triangles
    ======================================================================
    Available shape options:
    1 - AAS (Known: Angle - Angle - Side)
    2 - ASA (Known: Angle - Side - Angle)
    3 - SAS (Known: Side - Angle - Side)
    4 - SSS (Known: Side - Side - Side)
    Q - Quit
    What type of triangle do you want to process? 4
    SSS Shape
    ----------------------------------------------------------------------
    Known Values:  All 3 sides
    Sought Values: All 3 angles
    ----------------------------------------------------------------------
    Type the known values
    SSS Side 1:  4.5
    SSS Side 2:  3.5
    SSS Side 3:  2.9
    ======================================================================
    Results
    ----------------------------------------------------------------------
    SSS Angle 1: 88.84271591186852
    SSS Angle 2: 51.043098746872104
    SSS Angle 3: 40.11418534125937
    ======================================================================
    
    Press any key to close this window . . .
  11. To start debugging again, on the main menu, click Debug -> Step Into
  12. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twelve times until the debugging focus moves to the DOS window
  13. When the type of triangle is requested in the DOS window, type 1 and press Enter
  14. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice that the debugging focus has moved where the CalculateAAS() function is called
  15. To continue debugging, on the main menu of Microsoft Visual Studio, click Debug and click Step Into.
    Notice that the debugging focus has moved to the begining of the body of the CalculateAAS() function.
    Notice that the Locals window is showing the variables declared in the CalculateAAS() function:

    Debugging Functions

  16. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice the content of the Locals window where the variables are still showing default values
  17. To continue debugging, on the Debug toolbar, click the Step Into button Step Into ten times.
    Notice where the debugging focus is currently
  18. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice that the debugging focus has moved to the function that was called
  19. In the DOS window where AAS Angle 1 is requested, type 80 and press Enter
  20. To continue debugging, on the Debug toolbar, click the Step Into button Step Into four times
  21. When the value of the AAS Angle 2 is requested in the DOS window, type 75 and press Enter
  22. To continue debugging, on the Debug toolbar, click the Step Into button Step Into four times
  23. When the value of the AAS Side 1 is requested in the DOS window, type 14 and press Enter

    Debugging Functions - The Autos Window

  24. To continue debugging, on the Debug toolbar, click the Step Into button Step Into ten times.
    Every time you click the Step Into button, position the mouse on the variables in the CalculateAAS() function.
    Also check the values in the Locals and the Autos window:

    Debugging Functions

  25. To continue debugging, on the Debug toolbar, click the Step Into button Step Into
    ======================================================================
    This application allows you to make calculations on oblique triangles
    ======================================================================
    Available shape options:
    1 - AAS (Known: Angle - Angle - Side)
    2 - ASA (Known: Angle - Side - Angle)
    3 - SAS (Known: Side - Angle - Side)
    4 - SSS (Known: Side - Side - Side)
    Q - Quit
    What type of triangle do you want to process? 1
    AAS Shape
    ----------------------------------------------------------------------
    Known Values:  Known Values: 2 sides and the 1 angle between them
    Sought Values: 2 angles and the 1 sides between them
    ----------------------------------------------------------------------
    AAS Angle 1: 80
    AAS Angle 2: 75
    AAS Side 1:  14
    ======================================================================
    Results
    ----------------------------------------------------------------------
    AAS Angle 3:  25
    AAS Side  2:  13.731575047703062
    AAS Side  3:  6.0079296149656205
    ======================================================================
    
    Press any key to close this window . . .
  26. In the DOS window, press R to close the window and return to your programming environment
  27. Change the code in the Program.cs document as follows:
    using static System.Console;
    
    double GetSASAngle1()
    {
        double angle = 0;
        
        try
        {
            Write("SAS Angle 1: ");
            angle = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the known angle of the SAS shape.");
        }
    
        return angle;
    }
    
    double GetSASSide1()
    {
        double side = 0;
    
        try
        {
            Write("SAS Side  1: ");
            side = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known sides of the SAS shape.");
        }
    
        return side;
    }
    
    double GetSASSide2()
    {
        double side = 0;
    
        try
        {
            Write("SAS Side  2: ");
            side = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the other known side of the SAS shape.");
        }
    
        return side;
    }
    
    void CalculateAAS()
    {
        double angle1 = 0.00;
        double angle2 = 0.00;
        double side1  = 0.00;
    
        WriteLine("AAS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  Known Values: 2 sides and the 1 angle between them");
        WriteLine("Sought Values: 2 angles and the 1 sides between them");
        WriteLine("----------------------------------------------------------------------");
    
        try
        {
            Write("AAS Angle 1: ");
            angle1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the lower left angle of the AAS shape.");
        }
    
        try
        {
            Write("AAS Angle 2: ");
            angle2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the lower right angle of the AAS shape.");
        }
    
        try
        {
            Write("AAS Side 1:  ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the right side of the AAS shape.");
        }
    
        // Here, we use the law of sines
        double angle3 = 180 - (angle1 + angle2);
        double side2 = side1 * Math.Sin(angle2 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
        double side3 = side1 * Math.Sin(angle3 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("AAS Angle 3:  " + angle3.ToString());
        WriteLine("AAS Side  2:  " + side2.ToString());
        WriteLine("AAS Side  3:  " + side3.ToString());
    }
    
    void CalculateASA()
    {
        double angle1 = 0.00;
        double angle2 = 0.00;
        double side1  = 0.00;
    
        WriteLine("ASA Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  2 angles and the 1 side joining them");
        WriteLine("Sought Values: 2 sides and the 1 angle between them");
        WriteLine("----------------------------------------------------------------------");
    
        try
        {
            Write("ASA Angle 1: ");
            angle1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known angles of the ASA shape.");
        }
    
        try
        {
            Write("ASA Side 1:  ");
            side1 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the side that is between the angles whose values are about the ASA shape.");
        }
    
        try
        {
            Write("AAS Angle 2: ");
            angle2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the other known angle of the ASA oblique triangle.");
        }
    
        // Here, we use the law of sines
        double angle3 = 180 - (angle1 + angle2);
        double side2 = side1 * Math.Sin(angle2 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
        double side3 = side1 * Math.Sin(angle3 * Math.PI / 180) / Math.Sin(angle1 * Math.PI / 180);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("ASA Angle 3: {0}", angle3);
        WriteLine("ASA Side  2:  {0}", side3);
        WriteLine("ASA Side  3:  {0}", side2);
    }
    
    void CalculateSAS()
    {
        WriteLine("SAS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  2 sides and 1 angle");
        WriteLine("Sought Values: 1 unknown side and 2 unknown angles");
        WriteLine("----------------------------------------------------------------------");
    
        WriteLine("Type the known values");
    
        double side1 = GetSASSide1();
    
        double angle1 = GetSASAngle1();
    
        double side2 = GetSASSide2();
    
        // Here, we use the law of cosines
        double side3 = Math.Sqrt((side1 * side1) +
                                 (side2 * side2) -
                                 (2 * side1 * side2 * Math.Cos(angle1 * Math.PI / 180)));
        double angle2 = Math.Acos(((side3 * side3) +
                                   (side2 * side2) -
                                   (side1 * side1)) /
                                   (2 * side3 * side2)) * 180 / Math.PI;
        double angle3 = 180 - (angle1 + angle2);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("SAS Side  3: {0}", side3.ToString());
        WriteLine("SAS Angle 2: {0}", angle2.ToString());
        WriteLine("SAS Angle 3: {0}", angle3.ToString());
    }
    
    void CalculateSSS()
    {
        WriteLine("SSS Shape");
        WriteLine("----------------------------------------------------------------------");
        WriteLine("Known Values:  All 3 sides");
        WriteLine("Sought Values: All 3 angles");
        WriteLine("----------------------------------------------------------------------");
    
        double side1 = 0.00;
        double side2 = 0.00;
        double side3 = 0.00;
    
        WriteLine("Type the known values");
    
        try
        {
            Write("SSS Side 1:  ");
            side1 = double.Parse(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for one of the known sides of the SSS shape.");
        }
    
        try
        {
            Write("SSS Side 2:  ");
            side2 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for another side of the SSS triangle.");
        }
    
        try
        {
            Write("SSS Side 3:  ");
            side3 = Convert.ToDouble(ReadLine());
        }
        catch (FormatException)
        {
            WriteLine("You must type a value for the remaining known side of the SSS oblique triangle.");
        }
    
        // Here, we use the law of cosines
        double angle1 = Math.Acos(((side3 * side3) +
                                   (side2 * side2) -
                                   (side1 * side1)) /
                                   (2 * side3 * side2)) * 180 / Math.PI;
        double angle2 = Math.Acos(((side3 * side3) +
                                   (side1 * side1) -
                                   (side2 * side2)) /
                                   (2 * side3 * side1)) * 180 / Math.PI;
        double angle3 = 180 - (angle1 + angle2);
    
        WriteLine("======================================================================");
        WriteLine("Results");
        WriteLine("----------------------------------------------------------------------");
        WriteLine($"SSS Angle 1: {angle1}");
        WriteLine($"SSS Angle 2: {angle2}");
        WriteLine($"SSS Angle 3: {angle3}");
    }
    
    WriteLine("======================================================================");
    WriteLine("This application allows you to make calculations on oblique triangles");
    WriteLine("======================================================================");
    
    Title = "Oblique Triangles";
    
    WriteLine("Available shape options:");
    WriteLine("1 - AAS (Known: Angle - Angle - Side)");
    WriteLine("2 - ASA (Known: Angle - Side - Angle)");
    WriteLine("3 - SAS (Known: Side - Angle - Side)");
    WriteLine("4 - SSS (Known: Side - Side - Side)");
    WriteLine("Q - Quit");
    Write("What type of triangle do you want to process? ");
    string selection = ReadLine()!;
    
    switch (selection)
    {
        case null:
            break;
        case "4":
            CalculateSSS();
            break;
        case "3":
            CalculateSAS();
            break;
        default:
            break;
        case "1":
            CalculateAAS();
            break;
        case "2":
            CalculateASA();
            break;
    }
    
    WriteLine("======================================================================");
  28. To start debugging, in the Solution Explorer, right-click the name of the project -> Debug -> Step Into New Instance.
    Move the DOS window so you can see both your code and that window
  29. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twelve times
  30. When the type of triangle is requested in the DOS window, type 3 and press Enter
  31. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice that the debugging focus has moved where the CalculateSAS() function is called
  32. To continue debugging, on the main menu of Microsoft Visual Studio, click Debug and click Step Into.
    Notice that the debugging focus has moved to the begining of the body of the CalculateSAS() function

Options on Debugging a Function

The Return Value of a Function

When going through a debugging session, if your code includes a function that returns a value, you can monitor the return value of a function. You can check such a value before the function is called, while the function has the debugging focus, or after the function has been called.

Practical LearningPractical Learning: Checking the Return Value of a Function

  1. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice the content of the Locals window where the variables are still showing default values
  2. To continue debugging, on the Debug toolbar, click the Step Into button Step Into six times.
    Notice that the debugging focus is where the GetSASSide1() function is called.
    Position the mouse on the side1 variable of the "double side1 = GetSASSide1();" line to see its value
  3. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice that the debugging focus has moved to the begining of the body of the GetSASSide1() function.
    Inside that GetSASSide1() function, position the mouse on the "side" variable to see its value
  4. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times.
    Every time you activate the Step Into button, position the mouse on the "side" variable.
    Also check the contents of both the Autos and the Locals windows
  5. In the DOS window where the SAS Side 1 value is requested, type 15 and press Enter
  6. Inside that GetSASSide1() function, position the mouse on the "side" variable to see its value.
    Also check the contents of both the Autos and the Locals windows
  7. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twice.
    Notice the value in the closing curly bracket of the function:

    Debugging Functions

  8. To continue debugging, on the Debug toolbar, click the Step Into button Step Into eight times.
    Every time you activate the Step Into button, position the mouse on the "side" variable.
    Also check the contents of both the Autos and the Locals windows
  9. When the SAS Angle 1 value is requested in the DOS window, type 28 and press Enter
  10. Inside that GetSASAngle1() function, position the mouse on the "angle" variable to see its value.
    Also check the contents of both the Autos and the Locals windows
  11. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twice.
    Notice the value in the closing curly bracket of the function:

    Debugging Functions

  12. To continue debugging, on the Debug toolbar, click the Step Into button Step Into eight times.
    Every time you activate the Step Into button, position the mouse on the "side" variable.
    Also check the contents of both the Autos and the Locals windows
  13. When the SAS Side 2 value is requested in the DOS window, type 43.6 and press Enter
  14. Inside that GetSASSide2() function, position the mouse on the "side" variable to see its value.
    Also check the contents of both the Autos and the Locals windows
  15. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twice.
    Notice the value in the closing curly bracket of the function
  16. To continue debugging, on the Debug toolbar, click the Step Into button Step Into eleven times.
    Every time you activate the Step Into button, position the mouse on the "side" variable.
    Also check the contents of both the Autos and the Locals windows:

    Debugging Functions

    ======================================================================
    This application allows you to make calculations on oblique triangles
    ======================================================================
    Available shape options:
    1 - AAS (Known: Angle - Angle - Side)
    2 - ASA (Known: Angle - Side - Angle)
    3 - SAS (Known: Side - Angle - Side)
    4 - SSS (Known: Side - Side - Side)
    Q - Quit
    What type of triangle do you want to process? 3
    SAS Shape
    ----------------------------------------------------------------------
    Known Values:  2 sides and 1 angle
    Sought Values: 1 unknown side and 2 unknown angles
    ----------------------------------------------------------------------
    Type the known values
    SAS Side  1: 15
    SAS Angle 1: 28
    SAS Side  2: 43.6
    ======================================================================
    Results
    ----------------------------------------------------------------------
    SAS Side  3: 31.16190861517509
    SAS Angle 2: 13.06071118397518
    SAS Angle 3: 138.93928881602483
    ======================================================================
    
    Press any key to close this window . . .
  17. To create a new folder, in the Solution Explorer, right-click the name of the project -> Add -> New Folder
  18. Type Models as the name of the new folder
  19. In the Solution Explorer, right-click , right-click Models -> Add -> New Class...
  20. Change the file name to ConstantMessages
  21. Click Add
  22. Change the document and class as follows:
    namespace ObliqueTriangles2.Models
    {
        internal static class ConstantMessages
        {
            public const string GeneralIntroduction = "This application allows you to make calculations on oblique triangles";
            public const string AASIntroduction     = "Known Values:  Known Values: 2 sides and the 1 angle between them";
            public const string AASDescription      = "Sought Values: 2 angles and the 1 sides between them";
    
            public const string ASAIntroduction     = "Known Values:  2 angles and the 1 side joining them";
            public const string ASADescription      = "Sought Values: 2 sides and the 1 angle between them";
    
            public const string SASIntroduction     = "Known Values:  2 sides and 1 angle";
            public const string SASDescription      = "Sought Values: 1 unknown side and 2 unknown angles";
    
            public const string SSSIntroduction     = "Known Values:  All 3 sides";
            public const string SSSDescription      = "Sought Values: All 3 angles";
    
            public const string AASAngle1           = "You must type a value for the lower left angle of the AAS shape.";
            public const string AASAngle2           = "You must type a value for the lower right angle of the AAS shape.";
            public const string AASSide1            = "You must type a value for the right side of the AAS shape.";
    
            public const string ASAAngle1           = "You must type a value for one of the known angles of the ASA shape.";
            public const string ASAAngle2           = "You must type a value for the other known angle of the ASA oblique triangle.";
            public const string ASASide1            = "You must type a value for the side that is between the angles whose values are about the ASA shape.";
    
            public const string SASSide1            = "You must type a value for one of the known sides of the SAS shape.";
            public const string SASAngle1           = "You must type a value for the known angle of the SAS shape.";
            public const string SASSide2            = "You must type a value for the other known side of the SAS shape.";
    
            public const string SSSSide1            = "You must type a value for one of the known sides of the SSS shape.";
            public const string SSSSide2            = "You must type a value for another side of the SSS triangle.";
            public const string SSSSide3            = "You must type a value for the remaining known side of the SSS oblique triangle.";
    
            public const string SingleLine          = "----------------------------------------------------------------------";
            public const string DoubleLine          = "======================================================================";
        }
    }
  23. Click the Program.cs tab and change the document as follows:
    using static System.Console;
    using ObliqueTriangles2.Models;
    
    double GetValue(string identity, string msg)
    {
        double value = default;
    
        try
        {
            Write(identity);
            value = Convert.ToDouble(ReadLine()!);
        }
        catch (FormatException)
        {
            WriteLine(msg);
        }
    
        return value;
    }
    
    (double first, double second, double third) Calculate(TriangleType category, double known1, double known2, double known3)
    {
        double value1 = 0.00;
        double value2 = 0.00;
        double value3 = 0.00;
    
        switch (category)
        {
            case TriangleType.AngleAngleSide:
                // Here, we use the law of sines
                value1 = 180 - (known1 + known2);
                value2 = known3 * Math.Sin(known2 * Math.PI / 180) / Math.Sin(known1 * Math.PI / 180);
                value3 = known3 * Math.Sin(value1 * Math.PI / 180) / Math.Sin(known1 * Math.PI / 180);
    
                return (value1, value2, value3);
    
            case TriangleType.AngleSideAngle:
                // Here, we use the law of sines
                value1 = 180 - (known1 + known3);
                value2 = known2 * Math.Sin(known3 * Math.PI / 180) / Math.Sin(known1 * Math.PI / 180);
                value3 = known2 * Math.Sin(value1 * Math.PI / 180) / Math.Sin(known1 * Math.PI / 180);
    
                return (value1, value2, value3);
    
            case TriangleType.Unknown:
                return (0.00, 0.00, 0.00);
    
            case TriangleType.SideAngleSide:
                // Here, we use the law of cosines
                value1 = Math.Sqrt( (known1 * known1) +
                                    (known3 * known3) -
                                    (2 * known1 * known3 * Math.Cos(known2 * Math.PI / 180)));
                value2 = Math.Acos(((value1 * value1) +
                                    (known3 * known3) -
                                    (known1 * known1)) /
                                    (2 * value1 * known3)) * 180 / Math.PI;
                value3 = 180 - (known2 + value2);
                
                return (value1, value2, value3);
                
            default:
                return (0.00, 0.00, 0.00);
    
            case TriangleType.SideSideSide:
                // Here, we use the law of cosines
                value1 = Math.Acos(((known3 * known3) +
                                    (known2 * known2) -
                                    (known1 * known1)) /
                                    (2 * known3 * known2)) * 180 / Math.PI;
                value2 = Math.Acos(((known3 * known3) +
                                    (known1 * known1) -
                                    (known2 * known2)) /
                                    (2 * known3 * known1)) * 180 / Math.PI;
                value3 = 180 - (value1 + value2);
    
                return (value1, value2, value3);
        }
    }
    
    void CalculateAAS()
    {
        double angle1 = 0.00;
        double angle2 = 0.00;
        double side1  = 0.00;
    
        WriteLine("AAS Shape");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine(ConstantMessages.AASIntroduction);
        WriteLine(ConstantMessages.AASDescription);
        WriteLine(ConstantMessages.SingleLine);
    
        angle1 = GetValue("AAS Angle 1: ", ConstantMessages.AASAngle1);
        angle2 = GetValue("AAS Angle 2: ", ConstantMessages.AASAngle2);
        side1  = GetValue("AAS Side 1:  ", ConstantMessages.AASSide1);
    
        (double angle3, double side2, double side3) results = Calculate(TriangleType.AngleAngleSide, angle1, angle2, side1);
    
        WriteLine(ConstantMessages.DoubleLine);
        WriteLine("Results");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine("AAS Angle 3:  " + results.angle3.ToString());
        WriteLine("AAS Side  2:  " + results.side2.ToString());
        WriteLine("AAS Side  3:  " + results.side3.ToString());
    }
    
    void CalculateASA()
    {
        double angle1 = 0.00;
        double angle2 = 0.00;
        double side1  = 0.00;
    
        WriteLine("ASA Shape");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine(ConstantMessages.ASAIntroduction);
        WriteLine(ConstantMessages.ASADescription);
        WriteLine(ConstantMessages.SingleLine);
    
        angle1 = GetValue("ASA Angle 1: ", ConstantMessages.ASAAngle1);
        side1  = GetValue("ASA Side 1:  ", ConstantMessages.ASASide1);
        angle2 = GetValue("ASA Angle 2: ", ConstantMessages.ASAAngle2);
    
        (double angle3, double side2, double side3) results = Calculate(TriangleType.AngleSideAngle, angle1, side1, angle2);
    
        WriteLine(ConstantMessages.DoubleLine);
        WriteLine("Results");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine("ASA Angle 3:  {0}", results.angle3);
        WriteLine("ASA Side  2:  {0}", results.side2);
        WriteLine("ASA Side  3:  {0}", results.side3);
    }
    
    void CalculateSAS()
    {
        WriteLine("SAS Shape");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine(ConstantMessages.SASIntroduction);
        WriteLine(ConstantMessages.SASDescription);
        WriteLine(ConstantMessages.SingleLine);
    
        WriteLine("Type the known values");
    
        double side1  = GetValue("SAS Side  1: ", ConstantMessages.SASSide1);
        double angle1 = GetValue("SAS Angle 1: ", ConstantMessages.SASAngle1);
        double side2  = GetValue("SAS Side  2: ", ConstantMessages.SASSide2);
    
        (double side3, double angle2, double angle3) results = Calculate(TriangleType.SideAngleSide, side1, angle1, side2);
    
        WriteLine(ConstantMessages.DoubleLine);
        WriteLine("Results");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine("SAS Side  3: {0}", results.side3);
        WriteLine("SAS Angle 2: {0}", results.angle2);
        WriteLine("SAS Angle 3: {0}", results.angle3);
    }
    
    void CalculateSSS()
    {
        WriteLine("SSS Shape");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine(ConstantMessages.SSSIntroduction);
        WriteLine(ConstantMessages.SSSDescription);
        WriteLine(ConstantMessages.SingleLine);
    
        double side1 = 0.00;
        double side2 = 0.00;
        double side3 = 0.00;
    
        WriteLine("Type the known values");
    
        side1 = GetValue("SSS Side 1:  ", ConstantMessages.SSSSide1);
        side2 = GetValue("SSS Side 2:  ", ConstantMessages.SSSSide2);
        side3 = GetValue("SSS Side 3:  ", ConstantMessages.SSSSide3);
    
        (double angle1, double angle2, double angle3) results = Calculate(TriangleType.SideSideSide, side1, side2, side3);
    
        WriteLine(ConstantMessages.DoubleLine);
        WriteLine("Results");
        WriteLine(ConstantMessages.SingleLine);
        WriteLine($"SSS Angle 1: {results.angle1}");
        WriteLine($"SSS Angle 2: {results.angle2}");
        WriteLine($"SSS Angle 3: {results.angle3}");
    }
    
    WriteLine(ConstantMessages.DoubleLine);
    WriteLine(ConstantMessages.GeneralIntroduction);
    WriteLine(ConstantMessages.DoubleLine);
    
    Title = "Oblique Triangles";
    
    WriteLine("Available shape options:");
    WriteLine("1 - AAS (Known: Angle - Angle - Side)");
    WriteLine("2 - ASA (Known: Angle - Side - Angle)");
    WriteLine("3 - SAS (Known: Side - Angle - Side)");
    WriteLine("4 - SSS (Known: Side - Side - Side)");
    WriteLine("Q - Quit");
    Write("What type of triangle do you want to process? ");
    string selection = ReadLine()!;
    
    switch (selection)
    {
        case null:
            break;
        case "4":
            CalculateSSS();
            break;
        case "3":
            CalculateSAS();
            break;
        default:
            break;
        case "1":
            CalculateAAS();
            break;
        case "2":
            CalculateASA();
            break;
    }
    
    WriteLine(ConstantMessages.DoubleLine);
    
    public enum TriangleType
    {
        Unknown        = 0,
        AngleAngleSide = 100,
        AngleSideAngle = 200,
        SideAngleSide  = 500,
        SideSideSide   = 700
    }
  24. To start debugging, in the Solution Explorer, right-click the name of the project -> Debug -> Step Into New Instance
  25. To continue debugging, on the Debug toolbar, click the Step Into button Step Into twelve times
  26. When the type of triangle is requested in the DOS window, type 2 and press Enter
  27. To continue debugging, on the Debug toolbar, click the Step Into button Step Into.
    Notice that the debugging focus has moved where the CalculateASA() function is called
  28. To continue debugging, on the main menu of Microsoft Visual Studio, click Debug and click Step Into.
    Notice that the debugging focus has moved to the begining of the body of the CalculateASA() function

The Parameters of a Function

As you know already, you can create a function that uses one or more parameters. When degugging a document that has a function that uses at least one parameter, the debugger can give you information about the value of the parameter. you can get such information before the function is called, while a function is being accessed, and after the function has been called.

Practical LearningPractical Learning: Debugging the Parameters of a Function

  1. To continue debugging, on the Debug toolbar, click the Step Into button Step Into ten times.
    Notice that the debugging focus is on the beginning of the body of the GetValue(string identity, string msg) function.
    Notice the expressions that appear on the right side of the closing parenthesis of the function.
    Also notice the values in the Locals window:

    Debugging Functions

  2. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times
  3. When the ASA Angle 1 value is requested in the DOS window, type 75 and press Enter

    Debugging Functions

  4. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times.
    Notice that the debugging focus moved again the beginning of the body of the GetValue(string identity, string msg) function.
    Notice the expressions that appear on the right side of the closing parenthesis of the function.
    Also notice the values in the Locals window
  5. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times
  6. When the ASA Side 1 value is requested in the DOS window, type 50 and press Enter:

    Debugging Functions

  7. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times.
    Notice that the debugging focus moved again the beginning of the body of the GetValue(string identity, string msg) function.
    Notice the expressions that appear on the right side of the closing parenthesis of the function.
    Also notice the values in the Locals window
  8. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times
  9. When the ASA Angle 2 value is requested in the DOS window, type 6 and press Enter:

    Debugging Functions

  10. To continue debugging, on the Debug toolbar, click the Step Into button Step Into five times.
    Notice that the debugging focus moved again the beginning of the body of the (double first, double second, double third) Calculate(TriangleType category, double known1, double known2, double known3) function.
    Notice the expressions that appear on the right side of the closing parenthesis of the function.
    Also notice the values in the Locals window:

    Debugging Functions

  11. To continue debugging, on the Debug toolbar, click the Step Into button Step Into seventeen times.
    Every time you click the Step Into button, position the mouse on various variables in the Calculate() function.
    Also, observe the variables and and their values in the Locals window
  12. To continue debugging, on the Debug toolbar, click the Step Into button Step Into three times:
    ======================================================================
    This application allows you to make calculations on oblique triangles
    ======================================================================
    Available shape options:
    1 - AAS (Known: Angle - Angle - Side)
    2 - ASA (Known: Angle - Side - Angle)
    3 - SAS (Known: Side - Angle - Side)
    4 - SSS (Known: Side - Side - Side)
    Q - Quit
    What type of triangle do you want to process? 2
    ASA Shape
    ----------------------------------------------------------------------
    Known Values:  2 angles and the 1 side joining them
    Sought Values: 2 sides and the 1 angle between them
    ----------------------------------------------------------------------
    ASA Angle 1: 75
    ASA Side 1:  50
    ASA Angle 2: 6
    ======================================================================
    Results
    ----------------------------------------------------------------------
    ASA Angle 3:  99
    ASA Side  2:  5.410791409793597
    ASA Side  3:  51.126510634345365
    ======================================================================
    
    Press any key to close this window . . .
  13. In the DOS window, press Y to close the window and return to your programming environment
  14. Close your programming environment

Previous Copyright © 2010-2026, FunctionX Sunday 23 November 2025, 12:44 Next