How to declare variable

How to declare variable for all void() as I have another void s in my C++ program.
I want to have a variable that can use for all the void and not only in a simple void.Is it possible?
closed account (Dy7SLyTq)
what??? do you mean function first of all?
You seem to be confused about the meaning of "void", try reading more about it.
Are you mean global variable?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int a = 1;//global variable

void fob(){
a = 2
cout << a  << endl;
};

void foc(){
cout << a  << endl;
};

int main()
{
 fob();
foc();
a = 4;
cout << a << endl
return 0;
};

Last edited on
@nkhau
Please could you try and explain your question a bit more clearly? It makes no sense at the moment.

@rizaado
Please use code tags when posting code.
let get an example:
i have:
the main void main()
void a()
void b()
void c()
and now i want to declare some variable or array in void main():
char name[80]
char array [4][2][80]={'\0'}

and now if I type something in my void a(),void b() or void c(), the variable name and array will not work in these void right, how to make those variable to be work inside my void a(),void b() and void c() continuously, hope somebody can solve my question, thanks.




Firstly, you seem to be confused about what the word "void" means.

main(), a(), b() and c() are functions. The word "void" here is the return type, and indicates that the functions don't return anything.

If you want other functions to have access to variables declared in main, then you need to pass them into those functions as parameters, e.g.:

1
2
3
4
5
6
7
8
void a(int i);  // Declaration of function called "a", takes int parameter called "i" and doesn't return anything

void main()  // main should really return an int, but whatever
{
  int myInt = 17;

  a(myInt);  // Passing the value of myInt into the function
}


With arrays, it's slightly more complicated, but the principle is the same.

Basically, you need to go back to your textbook and read up on functions.

(You can also use global variables. But global variables are problematic, and should be avoided wherever possible.)
Last edited on
if then the main is void main()
the variable for example :int1,int2; i want to use these in my another function for example: void second()
and if say i already get the variable int1=5 and int2=10 in my void main(),
how can i continuously use the saved variable to continue in my void second()?
hope you can explain in visual studio C++ programming, I am new in C++ and I have some difficult projects to do.Thank you.
As I've said, you define your function second to take two integer parameters. You then pass the values of int1 and int2 in when you call second.

Look, this is absolutely basic, fundamental stuff. Whatever textbook you're using to learn C or C++ from should explain it. Go and read it.
ok thanks, i get it, my book don't have it sorry.
Seriously? You have a C++ tutorial book that doesn't explain how to call a function with parameters?

I simply don't believe you. It's one of the most basic, rudimentary elements of C and C++. Programming in C++ would be impossible without knowing that. I simply don't believe that any book has been published that purports to teach C++ programming but doesn't tell you about something as fundamental and vital as function parameters.

But if, by some chance, you're telling the truth, then you've stumbled across the worst C++ tutorial book ever published. Burn it. Burn it, and scatter the ashes to the winds, because it will never teach you how to program in C++.
You can find a really good video tutorial series for basic c++ here: perfect for you,because you already are using visual studio.

http://xoax.net/cpp/crs/console/index.php

Take a look at Lesson 12.

Good luck,
Daylen

reply MikeyBoy: my tutorial book only teach those for, while if function etc, no explain how to call function with parameter.Because we haven't learn so far i think, now all my program use only void main

reply Daylen: thanks
reply MikeyBoy: my tutorial book only teach those for, while if function etc, no explain how to call function with parameter.

Then burn it. It will be impossible to learn C++ from a book that omits something as basic and as important as that.

I'm not joking. If you're telling the truth about the book, then it is no use to you at all.
Last edited on
reply MikeyBoy: but for program teach still useful, i will throw it if i already learnt all.
No. If you're telling the truth, then it's missing out vital information. It is useless.

Throw it away and get a better book.
The book is useless. It doesn't make sense. Might as well go back and program in assembly.
> now all my program use only void main

int main() { main() must return int


> my tutorial book only teach those for, while if function etc, no explain how to call function with parameter.
> Because we haven't learn so far i think

Any beginner's C++ book (including your book) will teach you how to call functions with parameters. Perhaps you haven't yet come to that part (which might be later in the book).

What is the name of the book (and author) you are trying to learn from? Without knowing that, an informed opinion of the book is impossible.

Topic archived. No new replies allowed.