In C++, a parameter is a variable that is defined while creating a function. When a function is called, these variables either take or receive arguments. An argument in C++ is the value that is passed to a function whenever that specific function is called.
Furthermore, a parameter is specified inside a parenthesis ()
right
after the function name. We can add as many parameter values as we wish to inside a function.
To pass a parameter and/or argument values to a function, we use the following syntax:
void nameOfFunction(parameter1, parameter2){// block of code to be executed}intmain(){ // calling the functionnameOfFunction(argument1, argument2);return 0;}
In the code below, we will create a function that takes a single parameter. Whenever we call the function, we pass an argument to the function.
#include <iostream>#include <string>using namespace std;void myFunction(string myname) {cout << myname << " Onyejiaku\n";}int main() {// calling the functionmyFunction("Theophilus");myFunction("Chidalu");return 0;}
myfunction
and pass a parameter myname
to the function."Theophilus"
to the function."Chidalu"
to the function.