+ 1

Output with reason

#include <iostream> using namespace std; int func(int) ; int num = 10 ; int main() { int num ; num = 5 ; cout << num ; cout << func(num) ; } int func(int x) { return num ; }

23rd Feb 2018, 6:44 AM
Hassan Ashiq
Hassan Ashiq - avatar
4 Réponses
+ 2
# include<...> using........ global function declared int func(int) global var declared int num = 10 main func() { ... another num variable declared..but only main can use it }// here main ends... outside main block..num means that top global var that is equal to10 func() { .....here returning num means returnig that global var. global var can be accessed to any block of function if there is no other same var name declared.. }
23rd Feb 2018, 7:30 AM
sayan chandra
sayan chandra - avatar
+ 1
#include <iostream> using namespace std; int func(int) ; int num = 10 ; // global variable num int main() { int num ; num = 5 ; // local var num for only main() func. cout << num ; // prints 5 cout << func(num) ; //calls func(5) ; argument that is passed is 5.. } int func(int x) { return num ; // from main thpugh argument that is passed is 5...its copied in x...here x is equal to 5,in this function there is no such declaration for num. so when it returns num this "num" is that global variable num that is 10..so it prints 10.. } ans is 510 as output.. ✌✌
23rd Feb 2018, 6:53 AM
sayan chandra
sayan chandra - avatar
+ 1
can you explain it more..sorry i didnt got it
23rd Feb 2018, 7:25 AM
Hassan Ashiq
Hassan Ashiq - avatar
+ 1
in main...again a same name variable is there... so it used that local main num...as 5 func has no other num var. so it uses globally declared num var.and prints 10
23rd Feb 2018, 7:31 AM
sayan chandra
sayan chandra - avatar