0

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

4
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by yb1pls
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.