In this example, we will see hello world program with User define function.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> // User-defined function to print "Hello, World!" void printHelloWorld() { std::cout << "Hello, World!" << std::endl; } int main() { // Call the user-defined function printHelloWorld(); return 0; } |
Explanation:
Component | Description |
Header File | #include <iostream> – Includes the standard input/output stream library. |
Namespace | using namespace std; – Uses the standard namespace to avoid std:: prefix. |
User-defined Function | void printHelloWorld() – Function definition to print “Hello, World!”. |
Main Function | int main() – Entry point of the program. |
Function Call | printHelloWorld(); – Calls the user-defined function within main(). |
Return Statement | return 0; – Ends the main function and returns 0 to the operating system. |
Variables | None |
Data Types | void – Return type of printHelloWorld() function. |
Output Statement | cout << “Hello, World!” << endl; – Outputs the string to the console. |