LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Find the Error · Question 10

Error: defining a function inside another function Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

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

main.cpp
#include <iostream>

int main() {
    int helper(int x) {
        return x + 1;
    }
    std::cout << helper(5) << '\n';
    return 0;
}
  • A helper must be declared static
  • B C++ does not allow function definitions inside other functions; define helper outside main or use a lambda Correct
  • C x must be a reference
  • D helper must return void
Correct Answer: Option B

A local lambda gives the same effect: auto helper = [](int x) { return x + 1; };.

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