Saturday, 31 October 2020

A Swift program on Optionals with functional return type

 In this program we are giving optional to the return values of the function


//Creating a function for Btech first year result using default parameters with optionals return

    func BTechFYResult(engMarks: UInt8, CLangMarks: UInt8, maths1Marks: UInt8 = 95, maths2Marks:UInt8 = 85, phyMarks:UInt8 = 98, BEEMarks:UInt8 = 70, EDCMarks:UInt8 = 65, drawingMarks:UInt8 = 85)->(result:Bool, total:UInt16, percentage:Float)?

    {

        var isFirstYear:Bool = true

        //Calculating the first year total and percentage

        let firstYearTotal:UInt16 = UInt16(engMarks) + UInt16(CLangMarks) + UInt16(maths1Marks) + UInt16(maths2Marks) + UInt16(phyMarks) + UInt16(BEEMarks) + UInt16(EDCMarks) + UInt16(drawingMarks)

        let firstYearPercentage:Float = (Float(firstYearTotal)*100)/Float(maxTotal)

        //Condition to check student is passed or failed

        if ((engMarks >= passMarks) && (CLangMarks >= passMarks) && (maths1Marks >= passMarks) && (maths2Marks >= passMarks) && (phyMarks >= passMarks) && (BEEMarks >= passMarks) && (EDCMarks >= passMarks) && (drawingMarks >= passMarks))

        {

            isFirstYear = true

        }else

        {

            isFirstYear = false

        }

        //Creating and storing the values in tuple

        var BTechFYResultSummary:(result:Bool, total:UInt16, percentage:Float) = (isFirstYear, firstYearTotal, firstYearPercentage)

        return BTechFYResultSummary

    }


//Creating a function for student results

    func shahinaResults()

    {

        //Creating and storing a variable

        var name = "SHAIK SHAHINA" // Type inference with string

        var shahinaResults = BTechFYResult(engMarks: 85, CLangMarks: 92 , maths1Marks: 86, maths2Marks: 66)

        //Unwrapping the optional return values and comparing

        if (((shahinaResults!.total) != nil&& ((shahinaResults!.percentage) != nil) )

        {

            //Condtion to check results

            if (shahinaResults!.result == true)

            {

                print("Conratulations \(name) you passed in the examination")

                print("Total Marks = \(shahinaResults!.total)\nPercentage = \(shahinaResults!.percentage)")

                percentage = shahinaResults!.percentage

                grades()

            }else

            {

                print("Sorry \(name) you have failed in the examination")

            }

        }

    }

No comments:

Post a Comment