简体   繁体   中英

Use of Unresolved Identifier for Function // Swift

UPDATE BELOW

I am currently in an introductory Swift course at my college and am struggling with functions. I thought I followed the instructions clearly, but I am getting a certain error about "use of an unresolved identifier."

This is the full error:

error: My Functions Playground 2 2.playground:23:8: error: use of unresolved identifier 'newGPA' switch newGPA {

Here is my code (the original instructions are below):

var gpa: Int
var attemptedHours: Int
var earnedGradePoints: Int

// create your function here
func gpaUpdater(hours moreHours: Int, grade moreGPA: Int) {
    let _: Int = attemptedHours + moreHours
    let newGPA: Int = gpa + moreGPA
    print(newGPA)
}

// call the function
gpaUpdater(hours: 16, grade: 60)

// add the new hours and grade points here and call the function again
switch newGPA {
case 0...1.8:
    print("You will be placed on suspension")
case 1.8...2.0:
    print("You will be placed on probation")
case 3.5...3.8:
    print("You will be put on the dean's list.")
case 3.9:
    print("You will be put on the president's list.")
default:
    print("Carry on. Nothing to see here.")
}

Instructions:

We're going to track your GPA from one semester to the next. Assume at the end of your sophomore years, you have attempted 60 hours and have earned 222.5 grade points. Assign attempted hours and grade points to variables. Write a function that updates your current GPA and assigns it to the GPA var (you'll update it along the way). Label your function arguments. Print your new GPA from within the function.

At the end of the current semester, add 16 hours and 60 grade points to your record. Call the gpa function to update your overall gpa.

Test your gpa at the end of the year to see if any administrative action needs to be taken. If the gpa is less than 1.8, the student will need to be placed on suspension. If less than 2.0, we need to put the student on probation. If over 3.5, we'll put the student on the dean's list, and if over 3.9, we'll put the student on the president's list. Create a switch that prints the recommended adminstrative action. If no action is required, print, "Carry on. Nothing to see here." Create internal and external labels for your arguments.

Thank You for your help!

UPDATE

The function part of my Swift code is now correct, thank you all for the help. Now I am trying to fix my switch statement. Here is my code:

// add the new hours and grade points here and call the function again
switch gpa {
case gpa > 1.8:
    print("You will be placed on suspension")
case 1.8...2.0:
    print("You will be placed on probation")
case 3.5...3.8:
    print("You will be put on the dean's list.")
case gpa > 3.9:
    print("You will be put on the president's list.")
default:
    print("Carry on. Nothing to see here.")
}

The problem, I think, is that my teacher wants GPA to be an int, but if I want to use values like 1.9 for the gpa, then it needs to be a double. Here is an error that I am getting:

error: My Functions Playground 2 2.playground:26:10: error: binary operator '>' cannot be applied to operands of type 'Int' and 'Double' case gpa > 1.8

Scope. Scope. Scope.

newGPA is declared locally in the scope of gpaUpdater . It's not visible on the top level.

You could do

// create your function here
func gpaUpdater(hours moreHours: Int, grade moreGPA: Int) -> Int {
    // let _: Int = attemptedHours + moreHours
    return gpa + moreGPA
}

// call the function
let newGPA = gpaUpdater(hours: 16, grade: 60)

// add the new hours and grade points here and call the function again
switch newGPA { ...

No comment about the (unused) first parameter of gpaUpdater and the floating point cases switching over an Int 😉

I'm going to answer this from an assignment perspective; the other answers regarding returning the local variable value are correct for accessing your newGPA variable.

You missed the point in the assignment by creating the "newGPA" variable. The assignment states to "update" the global gpa variable with the new value from within the function.

If this is introductory coding, you may not have come across the concept of recursion. This is basically assigning some value by including itself in the calculation.

Instead of

let newGPA: Int = gpa + moreGPA print(newGPA)

think

gpa = gpa + moreGPA
print(gpa)

which can also be written as

gpa += moreGPA

and then use gpa in your switch function.

What this does is updates your global gpa variable to a new value (by adding the moreGPA to it). This is one of the main strengths of a global variable. It can be accessed and modified from anywhere in your program.

That's my understanding based on the assignment instructions. That said, returning a value from a function is cleaner (in my opinion) since global variables can become conflicts in more complicated programs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM