Knowee
Questions
Features
Study Tools

#include <iostream> void modify(const int& num) { num = 10; } int main() { int a = 5; modify(a); std::cout << "a: " << a << std::endl; return 0; } (2 Points) No issue b) Incorrect reference type c) Modifying const variable d) Missing const in function prototype

Question

#include <iostream>

void modify(const int& num) {

num = 10;

}

int main() {

int a = 5;

modify(a);

std::cout &lt;&lt; &quot;a: &quot; &lt;&lt; a &lt;&lt; std::endl;

return 0;

} (2 Points)

No issue

b) Incorrect reference type

c) Modifying const variable

d) Missing const in function prototype

🧐 Not the exact question you are looking for?Go ask a question

Solution

The issue with the code is option c) Modifying const variable. In C++, when you declare a variable as const, you are telling the compiler that the variable is not going to change during the program execution. In the function modify(), the parameter num is passed as a const reference, which means it cannot be modified. However, the function tries to change the value of num, which is not allowed. Therefore, the code will result in a compilation error.

This problem has been solved

Similar Questions

0/0

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.