C++ Functions with No Arguments and No return value

This C++ program is used to demonstrates how to use a function without any parameters and return type.

#include <iostream>
using namespace std;

int main()
{
    void print(); //function declaration
    print(); //function call
    cout<<"\no parameter and no return type \n";
    print();
    return 0;
}

void print(void) //called function
{
     for (int i=1;i<35;i++)
     cout<<"*";
     cout<<"\n";
}

Explanation:
First of all, you have to include the iostream header file using the "include" preceding by # which tells that hat the header file needs to be process before compilation, hence named preprocessor directive.

Next is int main(). As you know that all the execution of any C++ program starts from main() function. So the main() function is defined with return type as integer. Now, you have to take a user defined function name and declare it within main() having no return type and hence void. Then you have to call that user defined function. And finally the return 0; statement is used to return an integer type value back to main().

Now in the function definition in this program you have to use a for loop with integer type variable I initialized with 1 till 35. Then on the next line, the cout statement will print asterisk. The next cout statement with the help of "\n" will take the cursor to the next line.


Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.