Posted on Leave a comment

Functions in C++

functions
Spread the love

C++ functions have (Except especial functions)

  • declaration: this is a statement of how the function is to be called.
  • definition: this is the statement(s) of the work the executing program will perform when called

C++ Functions

  • Accept parameters, but they are not required
  • Return values, but a return value is not required
  • Can modify parameters, if given explicit direction to do so

Function declaration in C++

void functionName();

syntax for the definition

void functionName()

{

     statement(s);

}

C++ does not allow arrays to be passed to functions, but, as we have seen, it does allow pointers to be passed.

Functions passing constant argument

int doubleInputFunc(const int input)

Function Declaration and Definition

  • Declaration: the function’s name, return type, and parameters (if any)
  • Definition: the body of the function (code to be executed)

void myFunction() { // declaration

  // the body of the function (definition)

}

Function overloading

  • With function overloading, multiple functions can have the same name with different parameters:

int myFunction(int z)

float myFunction(float z)

double myFunction(double z, double w)

Leave a Reply

Your email address will not be published. Required fields are marked *