Jump to content

Help in C++ functions


Rajnish Kaushik

Recommended Posts

I want to make an programme in c++ (Using Functions) to find the area of squre but i m not able to make it

so can someone help me giving some hints

my coading for function is

int area(int m)

int i,area;

area=i*i;

return(area)

}

but i m getting error that

1-i is assigned to value that is never used

2-m is assigned to a value that is never used

Link to comment
Share on other sites

If that's all what you really have, the most visible error is lack of bracket.

 

It should be

 

int area(int m) {

 

And error is obvious - you have uninitialized variable i

Shouldn't you multiply by m?

 

int calculate_area(int m)

{

return(m*m)

}

 

Better don't use the same name for variables and function name!


Difference between uninitialized variable:

 

int a;

 

and initialized:

 

 

int a = 0;

 

or

 

int a = b;

 

Initialization means assigning some value to it.

Uninitialized variable has random data so using it might result in crashing computer in the worst scenario.

Link to comment
Share on other sites

calculate_area() doesn't mean cal_area()...

They have to have the same name for both.

 

You should have functions declared ABOVE main() where they're used. Otherwise you have to make prototype. Prototype for your function would be line

int cal_area(int);

 

Get rid of this "int" where is cursor, it's causing error.

Link to comment
Share on other sites

It won't affect functionality of your code, but better place prototype declaration outside of main() function (before it).

In large sources, prototypes are usually put to separate files with .h extension (h = header). Then you can include them in multiple source files.

Edited by Sensei
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.