c++ function
#include<iostream> using namespace std; void square(int value) { int total = pow(value, 2); cout << total << endl; } void cube(int value) { int total = pow(value, 3); cout << total << endl; } int main() { int value; cout << "Enter value: "; cin >> value; if (value % 2 == 0) { square(value); } else { cube(value); } return 0; }
Source: stackoverflow.com
c++ function
// the void function void function() { cout << "Hello World!"; } // the main function int main() { function(); return 0; }
function c++ example
#include <iostream> using namespace std; // declaring a function void displayNum(int n1, float n2) { cout << "The int number is " << n1; cout << "The double number is " << n2; } int main() { int num1 = 5; double num2 = 5.5; // calling the function displayNum(num1, num2); return 0; }