•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,552 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 3,472 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: 851 | Replies: 3
![]() |
| |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
Hey. I'm having some problems with creating user defined functions.
Been chopping up this code for a while, hope someone can show me where I went wrong.
# 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 2:07 am.
Hi ,
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
•
•
•
•
Originally Posted by nano404
int perimeter (x,y);
There should be a function definition which corresponds to declaration.
greetings
Parthiban
Last edited by parthiban : Oct 17th, 2007 at 5:10 am.
>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:
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:
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:
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:
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:
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:
Function declarations are also called prototypes.
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>
}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;
}int main()
{
add ( 2, 3 ); // add hasn't been declared yet
}
int add ( int a, int b )
{
return a + b;
}int add ( int a, int b )
{
return a + b;
}
int main()
{
add ( 2, 3 ); // add hasn't been declared yet
}<return type> <function name> ( <parameter list> );
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;
} I'm here to prove you wrong.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Determine in which file a user function is defined? (PHP)
- User-defined functions (C++)
- User-Defined Function - part 2 (C++)
- help:stl vector of user defined objects (C)
- error in user defined string class (C++)
- using(STL)function object (bind2nd) with a user defined function object (C++)
- DECLARATION SYNTAX ERROR (for bc 31 user) (C++)
- User defined functions (C++)
Other Threads in the C++ Forum
- Previous Thread: help please
- Next Thread: cin.get() & cin.ignore()?!



Hybrid Mode