Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
185 views
in Functions by (30.4k points)
closed by

Read the following program 

# include<iostream.h>

int main()

{

cout<<sum(2, 3);

}

int sum(int x, int y)

{return (x + y);}

On compilation on the program, an error will be displayed. Identify and explain the reason. How can you rectify the problem

1 Answer

+1 vote
by (30.5k points)
selected by
 
Best answer

The compilation of the program starts from the first line and next line and so on( i.e. line by line). While compiling the line cout<<sum(2, 3); The compiler does not understand the word sum(2, 3) because it is not declared yet hence the error prototype required. To rectify this problem there are two methods

First method

Give the function definition just before the main function as follows. 

# include<iostream>

using namespace std;

int sum(int x, int y)

{return (x+y);}

int main()

{

cout<<sum(2, 3);

}

Second Method 

Give the function declaration(prototype only) in the main function as follows.

# include <iostream>

using namespace std;

int main()

{

int sum(int, int);

cout<<sum(2, 3);

}

int sum(int x, int y)

{return (x+y);}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...