This community-built FAQ covers the “Combining Step 1 and Step 2” exercise from the lesson “Variables”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Learn C++
FAQs on the exercise Combining Step 1 and Step 2
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (
) below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply (
) below!
Agree with a comment or answer? Like (
) to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
Is Uniform Initialization not allowed as a valid solution? It seems I can only use copy initialization. I can’t use direct either.
Does return work the same way in C++ as it does in JavaScript?
Very similar, but if you are referring to the return in this code from the exercise:
#include <iostream>
int main() {
int score = 0;
// Declare and initialize a variable here
int year = 2019;
return 0;
}
Every function in C++ either returns something, or must be declared with the keyword void
. In this example, int main()
is the declaration of the main()
function. Every C++ program must have a main() function that will be the entry point for the program when it is executed. The int
in the declaration is the data type for the return value. The last line of the function return 0;
tells the computer that the function executed successfully. All of this will become clear as you progress through the lessons. You will notice at some point that not every int main()
function has the return 0;
statement at the end. I believe that the main()
function is the only function where this is allowed. Most compilers add the return 0;
to the end of the function in the compiled code whether you type it or not. Hope this helps!
5 Likes
Thank you! Does the main()
function have to be declared with int
? If not, how would we return a value?
As I understand it, yes. return 0;
is the accepted standard exit code for successful execution. It may be that another program ran your program, and the value 0
is returned indicating everything executed successfully. Then the shell program that ran your program can decide what to do next.
2 Likes
Hello again! I have a new question - how does void
come into play?
The void keyword in a function declaration lets the computer know that the function isn’t going to return anything to the caller. Consider these two examples:
//Example 1 sayHello() returns a string
#include <iostream>;
std::string sayHello() { //declared with std::string
return "Hello World!"; //returns a string
}
int main() {
std::cout << sayHello(); //sayHello() is called, and the return value is printed to the console
return 0;
}
//Output:
Hello World!
//Example 1 sayHello() returns prints a message, but returns nothing
#include <iostream>;
void sayHello() { //declared with void
std::cout << "Hello World!"; // string value printed to the console
// Notice there is no return statement. There cannot be a return statement in a function declared with void.
}
int main() {
sayHello(); //sayHello() is called, but nothing is returned
return 0;
}
//Output:
Hello World!
Both examples do exactly the same thing, but in completely different ways. The first example returns data of type string to the line of code that called the function. In the second example, the function itself prints the string to the console, and returns nothing to the caller.
Hope this helps!
3 Likes
Thanks! One more question - in the first example you gave, in the body of the main()
function, the return value of sayHello()
is printed to the console, and then 0
is returned in the next line of code. Does this mean that when "Hello World!
, the return value of sayHello()
, is returned to the console it doesn’t also return to the function? Or maybe it is necessary to return 0
at the end of the main()
function as well?
Not exactly sure what you’re asking. “Hello World!” is the return value of the sayHello()
function, so std::cout <<
prints it to the console. If we wanted to do something with the return value other than print it to the console we could assign it to a variable, and then do any number of things. Instead of std::cout << sayHello();
we could use std::string myVariable = sayHello();
Now the return value is stored in myVariable
, and we can do whatever we want with it.
After that, 0
is returned to whatever shell program ran our example. The main()
function always returns something. If we leave return 0;
out, the compiler adds it to the machine code when it’s compiled. Essentially return 0;
tells the computer that this program has reached the end. The value 0 indicates that it executed successfully.
2 Likes
What does this actually mean:
Note: We only need to declare a variable one time! And it is highly suggested to initialize a variable before using it later.
?
Exactly what it says. We declare variables once:
int x; //declaration
// many lines of code
int x = 5; //will throw an error since x has already been declared
As far as initializing a variable before using it later being highly suggested, you may forget that you declared a variable without initializing it, and later get unexpected results when trying to re-assign the variable. For example:
#include <iostream>
int main() {
int total;
int nums[3] = {5, 9, 11};
for(int i = 0; i < 3; i++) {
total *= nums[i];
}
std::cout << total << std::endl;
return 0;
}
What would you expect to be printed?
This is quite new syntax to me. Since I know C# I think I know what it really means(except the last line).
The last line just prints total
to the console, and moves the cursor to the next line.
Will we still declare main() function with int even if it return string
Why the 0 doesn’t show up in the terminal
Because you did not print it.