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
700 views
in Functions by (29.6k points)
closed by

Read the following program 

#include<iostream>

using namespace std; 

int a = 0; 

int main() 

int showval(int); 

cout<< a; a++; 

cout << showval (a); 

cout<< a; 

}

int showval(int x) 

int a = 5; 

return (a + x); 

Write down the value displayed by the output of the above program with suitable explanation. What are the inferences drawn regarding the scope of variables?

1 Answer

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

The output is 061. 

Global variable: 

A variable declared out side of all functions it is known as global variable.

Local variable: 

A variable declared inside of a function it is known as local variable. If a variable declared inside a function(main or other) with the same name of a global variable. The function uses the value of local variable and does not use the value of the global variable.

Here int a = 0 is a global variable. In the main function the global variable ‘a’ is used. There is no local variable so the value of ‘a’, 0 is displayed. The statement ‘a++’ makes the value of ‘a’ is 1. It calls the function showval with argument ‘a = T.

The argument ‘x’ will get this value i.e. ‘x = 1 But in the function showval there is a local variable ‘a’ its value is 5 is used. So this function returns 6 and it will be displayed. After this the value 1 of the global variable ‘a’ will be displayed. Hence the result 061.

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.

...