User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,862 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,963 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 731 | Replies: 3
Reply
Join Date: Oct 2007
Posts: 2
Reputation: nano404 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nano404 nano404 is offline Offline
Newbie Poster

Help User defined functions

  #1  
Oct 17th, 2007
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.
Last edited by nano404 : Oct 17th, 2007 at 1:07 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2006
Location: India
Posts: 79
Reputation: parthiban is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: User defined functions

  #2  
Oct 17th, 2007
Hi ,
Originally Posted by nano404
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
Last edited by parthiban : Oct 17th, 2007 at 4:10 am.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,070
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: User defined functions

  #3  
Oct 17th, 2007
>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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: nano404 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nano404 nano404 is offline Offline
Newbie Poster

Re: User defined functions

  #4  
Oct 17th, 2007
Thanks alot both of you. I got the program to work. Thanks for all the explanations Narue.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC