Hey. I'm having some problems with creating user defined functions.

# include <iostream>
using namespace std;

int num1, num2, x, y;

int perimeter (x,y);

int main ()

{
	cout << "Enter the length and width of the rectangle: ";
	cin >> num1, num2;
	perimeter (num1,num2);
	cout << "The perimeter is equal to" << perimeter <<endl;

}


{
	int perim (x,y)
	perim = (x*y*2)
	return perimeter
}

Been chopping up this code for a while, hope someone can show me where I went wrong.

Hi ,

int perimeter (x,y);

while declaring functions you need to mention the data type with variable name(optional) but not just variable names.
There should be a function definition which corresponds to declaration.

greetings
Parthiban

>I'm having some problems with creating user defined functions.
A function definition has four parts: the return type, the function name, the parameter list, and the body. It looks like this:

<return type> <function name> ( <parameter list> )
{
  <body>
}

This declares and defines a function. The return type can be pretty much any type, with void being a special return type meaning "returns nothing". The function name is any valid identifier. The parameter list is a comma separated list of variable declarations wrapped in parentheses. The body is a collection of declarations, definitions, and executable statements wrapped in braces.

Here's a function called add that takes two integer parameters, adds them together in the body, and returns the result as an integer:

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

Everything in C++ must be declared before you use it (otherwise your code won't compile), and it must be defined before it gets used (otherwise your code won't link). An example of using a function before it's declared follows:

int main()
{
  add ( 2, 3 ); // add hasn't been declared yet
}

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

It might look like add was declared because it's in the same file, but C++ parses from the top down, so unless add was declared before main, you can't use it without getting a compilation error (or warning). This works:

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

int main()
{
  add ( 2, 3 ); // add hasn't been declared yet
}

Now, it's not always practical to define your functions before calling them, so C++ gives you a way to break the function into a declaration and a definition. A function declaration is identical to a definition except instead of a body, it ends with a semicolon:

<return type> <function name> ( <parameter list> );

This tells the compiler what the function's name is, what type it returns, and what parameters it takes. These things are required for compilation, but the body isn't necessary to compile code that calls the function.

Adding a declaration to the non-working code from before gives you this working example:

int add ( int a, int b );

int main()
{
  add ( 2, 3 );
}

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

Function declarations are also called prototypes.

Thanks alot both of you. I got the program to work. Thanks for all the explanations Narue.

This article has been dead for over six months. Start a new discussion instead.