Jump to content

Help in C++ functions

Featured Replies

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

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.

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.

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

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.