LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Find the Error · Question 7

Error: returning a value from a void function Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

void percentage(int got, int total) {
    return got * 100 / total;
}

int main() {
    std::cout << percentage(45, 50) << '\n';
    return 0;
}
  • A The division must use doubles
  • B A void function cannot return a value (and its result cannot be printed); the return type should be int or double Correct
  • C got must be const
  • D percentage must take references
Correct Answer: Option B

Change it to int percentage(int got, int total) – or double for a fractional result.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links