Fundamental
Hello, World!
See the very first C++ Code:
#include <iostream> // import header file(s)
using namespace std; // invoke and specify the namespace
int main() { // define main function
cout << "Hello, world!"; // print Hello, world!
return 0; // return 0,end main function
}
Run it with:
g++ -o hello hello.cpp
Here is the Java code as a comparison:
import some.java.packages; // import declaration(s)
public HelloWorld {
// the main method returns void and gets the argument
// args(String) for receiving CLI args
public static void main(String[] args) {
System.out.println("Hello. World");
// void return
}
}
The package that is imported in Java is called the header file in C++. The same as Java, Header files in C++ can be user-created or system built-in. Besides, C++ has a concept called namespace
. What is that?
Returning and Printing
Topics: Function call and return, return types
Below is a series of four printLyrics_v#
functions, each of which has a blank where the return type should be. For each function, determine
what the return type of the function should be,
what value, if any, is returned, and
what output, if any, will be produced if that function is called.
Is it appropriate for each of these functions to be named printLyrics
? Why or why not?
_____ printLyrics_v1() {
cout << "Havana ooh na na" << endl;
}
_____ printLyrics_v2() {
return "Havana ooh na na";
}
_____ printLyrics_v3() {
return "H";
}
_____ printLyrics_v4() {
return 'H';
}
Last updated
Was this helpful?