-2
#include <iostream>    
using namespace std;

void b();

int main() {

    int a = 10;
    b(); 
}

void b() {
    int a;
    cout<<"Int a="<<a;
}

I am looking to print the value of a in the main scope using a function, with my current code, it prints Int a=0. How can I achieve this?

  • 6
    Have you learned about passing parameters to functions yet? – TheUndeadFish Sep 23 '20 at 17:15
  • The important thing that's missing from this code, I think, is not passing a parameter to a function but returning a value from one. – Tim Randall Sep 23 '20 at 17:17
  • 2
    Yep, learning on your own probably isn't a lot of fun. That's why people wrote [books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to help other people understand the language. And you have a lot to learn yet. – Lukas-T Sep 23 '20 at 17:18
  • 1
    Learn what _scope_, _function parameters_ and _local variables_ mean.. – πάντα ῥεῖ Sep 23 '20 at 17:19
  • This should answer most of your question https://en.cppreference.com/w/cpp/language/scope – abcalphabet Sep 23 '20 at 17:23
  • You could always do ( I think ) printf( "A = %i", a ); –  Sep 23 '20 at 17:24
  • @thatfox And how should that solve the OPs problem? – πάντα ῥεῖ Sep 23 '20 at 17:27
  • 3
    @JonTrauntvein Well, such basic stuff is either already answered here, or belongs to a textbook. Stack Overflow isn't the right place for beginner tutorials. – πάντα ῥεῖ Sep 23 '20 at 17:34
  • 3
    @JonTrauntvein actually this question presents a complete example and explains the problem very well, thats more than average. Downvotes are impossible to understand and discussing them in comments is futile. I edited the title according to your suggestion – 463035818_is_not_an_ai Sep 23 '20 at 17:34
  • @πάνταῥεῖ Isn't he just trying to print the number 10 into a string? –  Sep 23 '20 at 17:37
  • @πάντα ῥεῖ I have to disagree with your assertion. I recall reading many years ago text written by Joel Spoelsky stating how the intent of this site is to provide a platform where programmers of all skill levels and experience can find answers to questions. In this case, I do agree that this is a basic, foundational principle that can help anybody. I noticed that you said "probably" with respect to whether the question was already answered. I would have to assert, however, that, quite often, it is difficult to construct a search string. I would rather encourage good questions. – Jon Trauntvein Sep 23 '20 at 17:42
  • 1
    It's an honest question. It is also complete and reasonably well posed. But it's also a a question easily solved by reading the first few chapters of any non-fraudulent text book. That makes it Not Useful. Does the good outweigh the bad? That's a personal decision each contributor needs to make. – user4581301 Sep 23 '20 at 17:50
  • @idclev463035818 True that it isn't my job to decide his/her reasons. but understand how much it can terrify a new contributor on SO –  Sep 23 '20 at 17:56
  • 1
    @JonTrauntvein Best to discuss at Meta Stack Overflow (or even Meta Stack Exchange) then, but not here. – πάντα ῥεῖ Sep 23 '20 at 19:43
  • Does this answer your question? [How to access variables defined and declared in one function in another function?](https://stackoverflow.com/questions/11783435/how-to-access-variables-defined-and-declared-in-one-function-in-another-function) – Codemaker2015 Sep 24 '20 at 05:01

6 Answers6

3

Don't declare an entirely new a inside b(). Pass the a from main to b() and then print that.

For example:

#include <iostream>

void b(int whatever_name_you_want_here);

int main()
{
    int a = 10;
    b(a);
}

void b(int whatever_name_you_want_here)
{
    std::cout << "Int a=" << whatever_name_you_want_here;
}
Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

//Change your code to the following and it will give you the result you're looking for. On your code there is no way to pass int a on the main to b(); unless b accepts a parameter of the type you want the function to output.

#include<iostream>
 
void b(int);

int main()

{

    int a = 10;
    b(a);
}
void b(int a){
    std::cout << "int a=" << a;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Moosorkh
  • 11
  • 2
0

What you want to achieve requires you to pass a value to a function. Let me give you an example on how to do that.

#include<iostream>

void print_value(int value){
    std::cout << "Value is: " << value << '\n';
}

int main(){

    int a = 5;
    print_value(a);
    return 0;
}

The only thing you are missing in your program is the parameter. I won't bother explaining the whole thing over here as there are numerous articles online. Here is a straightforward one. Refer to this to understand how functions work in C++

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    Even though I understand your will to teach best practices as early as possible, I don't think your advice about not using `using namespace std;` is relevant to the question, or the situation in general, as the sole library OP is including is . No naming conflicts can happen here – Vélimir Sep 23 '20 at 17:33
  • @Sho That is true, but when i started I got advised the best and possibly after some time if a person get's used to the statement he might not like putting `std::` everywhere, I think it is good to cultivate the habit early on –  Sep 23 '20 at 17:36
  • 2
    @Sho "no naming conflicts can happen here" are you sure? How about if i declare a `int cout = 0;` ? Though I agree that it is secondary to answer the question – 463035818_is_not_an_ai Sep 23 '20 at 17:36
  • @idclev463035818 Bad choice of words on my part :) – Vélimir Sep 23 '20 at 17:39
0

I guess the main problem is not being aware of something very important which is called scope! Scopes are usually opened by { and closed by }

unless you create a global variable, it is only known inside the scope it has been introduced (declared).

you declared the function b in global scope :

void b();

so after this every other function including main is aware of it and can use it.

but you declared the variable a inside the scope of main:

    int a = 5;

so only main knows it and can use it.

Please make note that unlike some other programming languages, names are not unique and not every part of the program recognize them in c and c++. So the part:

void b() {
    int a;

does not force the function b to recognize the a which was declared in main function and it is a new a.

so to correct this mistake simply give the value or reference of variable a to function b :

#include <iostream>    

void b(int&);

int main() {
    int a = 10;
    b(a); 
}

void b(int& a) {
    std::cout << "Int a=" << a << std::endl;
}

please also note that the a as argument of the function b is not the same a in the function main. The final tip is every argument for functions is known inside that function scope as it was declared inside the function scope!

AKL
  • 1,367
  • 7
  • 20
0
#include <iostream>

using namespace std;

void displayValue(int number) {
    cout<<"Number is = "<<number;
}

int main()
{
    int myValue = 77;
    displayValue(myValue);

    return 0;
}
Satyajit Bhatt
  • 211
  • 1
  • 7
0

Use pass by reference to access a variable which is declared in one function in another. Refer the below code to understand the use of reference variable,

void swapNums(int &x, int &y) {
  int z = x;
  x = y;
  y = z;
}

int main() {
  int firstNum = 10;
  int secondNum = 20;

  cout << "Before swap: " << "\n";
  cout << firstNum << secondNum << "\n";

  // Call the function, which will change the values of firstNum and secondNum
  swapNums(firstNum, secondNum);

  cout << "After swap: " << "\n";
  cout << firstNum << secondNum << "\n";

  return 0;
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81