c++ return value of a funciton code example

Example 1: function return with int c++

#include <iostream>
 
int getValueFromUser()
{
 	std::cout << "Enter an integer: ";
	int input{};
	std::cin >> input;  
 
	return input;
}
 
int main()
{
    int x{ getValueFromUser() }; // first call to getValueFromUser
    int y{ getValueFromUser() }; // second call to getValueFromUser
 
    std::cout << x << " + " << y << " = " << x + y << '\n';
 
    return 0;
}

Example 2: return function in cpp

void printChars(char c, int count) {
    for (int i=0; i<count; i++) {
       cout << c;
    }//end for
   
    return;  // Optional because it's a void function
}//end printChars

Tags:

Cpp Example