I am in need of some examples of writing functions. The way it is stated in my book is kinda confusing. I dont know if I have to return a value or not. I dont understand if it is necessary to use the general form of a function returning a value since I really dont understand the form that is used.

Could anyone give some examples of functions and returning value functions?

Kelly

Recommended Answers

All 3 Replies

Every function declaration should have the following structure:

return-type name(optional-parameters);

Every function definition should have the following structure:

return-type name(optional-parameters)
{
}

A function can return void if there's no meaningful return value, and it can accept no parameters if there's no need to:

int sum(int a, int b)
{
  return a + b;
}

void display(double d)
{
  cout<< d <<endl;
}

void panic()
{
  cerr<<"Something funky happened!"<<endl;
  exit(EXIT_FAILURE);
}

double pi()
{
  return 3.14159;
}

Note that a definition is also a declaration, so as long as you either declare or define the function before you use it, all will be well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.