how to create a function in c++ code example

Example 1: how to declare a function in c++

// function example
#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return r;
}

Example 2: How to make a function in C++

//first lets create a function
/*void is for starting something, anything after void will be the name of your
function which will be followed by () */
void yourFunction() {
//your code will be here, anything here will be the code in the yourFunction
  cout << "Functions"
}
//now we have to go to our main function, the only function the compiler reads
int main() {
  myFunction(); //you call the function, the code we put in it earlier will be executed
  return 0;
}

Example 3: function in c++

#include <iostream>

using namespace std;

void function(){
    cout << "I am a function!" << endl;
}

int main()
{
    function();

    return 0;
}

Example 4: functions in C++

void Hello() {
  std::cout << "Hello";
}

int main () {
  Hello();
}

Tags:

Cpp Example