Uploaded by vantando

APSC 160 Oct 14

advertisement
Announcements:
There was a typo on the Midterm that made it confusing. If you think it happened to you please
submit an issue request, and the course instructors will review it.
Today’s Class:
● Functions
● Parameters
● Abstraction
Correct Answer: D. B, Actual Parameters, is technically right, but we use it when we want to
call the functions.
Correct Answer: C. The upper function is if/else, so sometimes it returns 1, sometimes it
returns 0. However, the lower function has 2 return lines; only the first return line gets evaluated.
Because we’re assessing the same condition, whether x > 0, both the upper and lower function
will return 1.
Correct Answer: D. When we defined the function we stated that it’d take 1 input parameter.
However, when we called the function, we wrote increment_value(). This produce an error
because we did not assign a parameter to the function. The proper way of writing this is
increment_value(x).
While we did assign a value int x = 0 the line before, the computer can’t actually read that, it can
only read whatever is in the brackets.
It is recommended to name the two differently, with value in the function and x when we use it
again in main to avoid confusion.
Correct Answer: B. Same question as above, but the error is corrected. The two functions is
separated, x is increased in increment_value so it’s x= 1 in the function.
This same change is not passed to main, so in main, x = 0.
This type of error can be prevented by using different function names
Question 5: How can we fix this code so that x = 1 in both main and function?
Add return value; to line 6
Add x = increment_value(x); to line 11
Add int to line 13
Worksheet: Functions 2, due 11:59pm Wednesday Oct 20
Download