Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have created function and call above the main() function. It is successfully call the function in GCC compiler on Linux platform. I don't understand, how main function call my own function.
#include <iostream>
using namespace std;
int myFunc();
int ret = myFunc();
int main()
{
cout << ret << endl;
}
int myFunc()
{
int i = 10, j = 20, k;
k = i+j;
return k;
}
Global variables are initialized before main is called. Therefore the call to myFunc happens before main is called. Your main function doesn't call myFunc at all.
It would have been very obvious if you used a debugger and set breakpoints in the myFunc and main functions, and looking at the call stack.
As Some programmer dude explained, it is being called before the main function.
To not be confused, I suggest that you explicitly call the myFunc() in the main function:
#include <iostream>
using namespace std;
int myFunc();
int main()
{
int ret = myFunc();
cout << ret << endl;
}
int myFunc()
{
int i = 10;
int j = 20;
return i+j;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
#include<iostream>
#include<string>
using namespace std;
int test(int i) {
int a = i-1;
return a;
}
int main()
{
test(3);
}
I am not getting returned value on console screen
Expected that the return value would be shown on console
A quick code review:
#include<iostream> // Not used
#include<string> // Not used
using namespace std; // Bad Practice
int test(int i) {
int a = i-1;
return a;
}
int main()
{
test(3); // Function called, returned value discarded.
// No code is ever called to print.
}
What you could do if you want to see the return value printed:
int main() {
std::cout << test(3) << '\n';
}
This revised main() function will print the value returned by your test() function to stdout. It will utilize <iostream> to do so.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
When I declare anything inside an if statement it doesn't propagate out of it, moreover, if I have a variable outside, and i redeclare it inside if statements it lost it once the code ends the statement, how can I manage to globalize the scope of an if statement.
Redeclaring a variable in an inner scope creates a new variable.
#include <iostream>
int main()
{
int i = 1;
if (true)
{
int i = 42; // variable is re-declared
} // lifetime of inner i ends here
std::cout << i; // expected output 1
}
You can reference a variable in an inner scope that was declared outside without re-declaring it.
#include <iostream>
int main()
{
int i = 1;
if (true)
{
i = 42; // variable from outer scope is used
}
std::cout << i; // expected output 42
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm writing a monte carlo simulation and my 1st function is an input but it keeps kicking back an error stating that the variable "is not declared in this scope", I tried adding the variable type in the main and it still doesn't build. I then added the variable type in the function (cin>>rounds to cin>> int rounds), and the error changed but still doesn't work. Can anyone tell me what's going on and what I need to do get the function to work.
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
int getInput();
using namespace std;
int main (void){
//set up random
srand(time(NULL));
//1st function
getInput();
}
/* #description gets a valid user input
* #param output and input
*/
int getInput(){
cout<<"enter amount of rounds";
cin>> rounds; **(error is here on line 24 ("rounds not declared in
this scope")**
}
"is not declared in this scope"
This means that where you try to use your variable (i.e. rounds), it is not known. Declaring it inside main wouldn't help, since the scope of getInput != scope of main.
You have 4 possibilities:
Declare in main and send as an argument [will be in scope for main + getInput]
Declare inside getInput [will be in scope for getInput]
Declare as global (i.e. above main) [will be in scope for everyone]
Add extern and declare wherever you like [will be in scope for everyone]
Clarification: "will be in scope for..." means "from here on..."
Here are code snippets to show your options:
/* 1st option */
void foo(int x){
x = 1;
}
int main()
{
int x;
foo(x);
return 0;
}
/*************************************/
/* 2nd option */
void foo(){
int x;
x = 1;
}
int main()
{
foo();
return 0;
}
/*************************************/
/* 3rd option */
int x;
void foo(){
x = 1;
}
int main()
{
foo();
return 0;
}
/*************************************/
/* 4th option */
void foo(){
extern int x;
x = 1;
}
int main()
{
foo();
return 0;
}
int x;
In the I would change your code into something like this:
#include <iostream>
int getInput();
using namespace std;
int main (void){
...
int in = getInput();
...
}
/* #description gets a valid user input
* #param output and input
*/
int getInput(){
int rounds;
cout << "enter amount of rounds";
cin >> rounds;
return rounds; // dont forget to return :)
}
You will need to declare the variable (as int or long whatever desired) in the function as given below:
int getInput(){
int rounds;
cout<<"enter amount of rounds";
cin>> rounds; **(error is here on line 24 ("rounds not declared in
this scope")**
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is there some way I can have a global variable (in this case a vector) retain its contents throughout any functions? I'm trying to see if I can do this:
vector<string> collected_input; //global
void some_function{
string bla = "towel";
collected_input.push_back(bla); //collected_input gains "towel"
}
void some_otherfunction{
string xyz = "zyx"
collected_input.push_back(xyz); //collected_input gains "zyx"
}
int main(){
// print the contents of the collected_input vector
}
What you have shown will work just fine, provided main() is calling some_function() and some_otherfunction():
#include <ostream>
#include <vector>
#include <string>
using namespace std;
vector<string> collected_input;
void some_function()
{
string bla = "towel";
collected_input.push_back(bla);
}
void some_otherfunction()
{
string xyz = "zyx"
collected_input.push_back(xyz);
}
int main()
{
some_function();
some_otherfunction();
for (vector<string>::iterator iter = collected_input.begin();
iter != collected_input.end();
++iter)
{
cout << *iter << '\n';
}
return 0;
}
The code you posted will achieve what you are looking for. Your have a single instance of a vector (collected_input), which is used across multiple functions. Your vector is effectively global, and in fact it is possible for other source files to access it by declaring a vector of the same name using the extern keyword, although this is highly recommended against.
Of course, right now your program does nothing because your main() function does not contain any code. If you were to call both of your functions from main() and then print the vector, you will find that both functions successfully operated on the vector.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <iostream>
using namespace std;
void reference(int &ref){
cout << ref << endl;
}
void pointer(int *ref){
cout << *ref << endl;
}
int main(){
int *i = new int[1];
*i = 10;
reference(*i); // fine
reference(i); // why not compiling!!! why not referencing to my pointer??
pointer(i); // fine
}
I want to reference a pointer, as i can see i am allowed to reference value but not pointer, why??
An object of type int* cannot be automatically converted to int&.
I think you are looking for something like:
void reference(int& ref){
cout << ref << endl;
}
void reference(int*& ref){
cout << *ref << endl;
}
Then, you can use both:
int main(){
int *i = new int[1];
*i = 10;
reference(*i);
reference(i);
return 0;
}
This line
reference(i);
is trying to pass in a int * - not an ``int` variable. Hence will not compile.
See the signature of the function
First of all "crash" is a term you can only use after getting through compiler...
void reference(int &ref)
This function is taking reference to integer as its parameter while you are passing pointer to integer through
reference(i)
Change your function's signature to something like:-
void reference(int* &ref)
for this call to work. OR change call to something like:-
int i;
reference(i);
for this function to work.