简体   繁体   中英

How to access int a declared in main in another function?

#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?

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;
}

//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;
}

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++

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!

#include <iostream>

using namespace std;

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

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

    return 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM