Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
Problem Statement - Correcting error in function call by reference
Program given below has some incorrectly written function call, correct the mistake and rewrite the
program.
TC++ #2742
×
Ask AI About This Code
Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.
×
Test CPP
The code has been copied automatically for you in the clipboard. After that open any of the service and press cntrl-v to paste in the given code area. Needs a bit of tweaking as per the link.
iseven(5) is incorrectly written as the function is being called as a reference and here a constant integer has been provided. so the call must be given as below –
int x=5; and then if(iseven(x)) …..
The corrected program will be –
#include <iostream>
using namespace std;
int iseven(int& a)
{
if(a%2==0) return 1;
else return 0;
}
int main()
{
int x=5;
if(iseven(x)) cout<<"even";
else cout<<"odd";
return 0;
}
Notes
Please note that whenever we declare a variable say int x=0; we actually name the location name as x where the variable of integer type 0 is stored. So if a reference type is passed to a function we can safely pass the variable name (which is actually an address location) and not the constant/literal value directly.