-5

We are starting to learn functions and I've copied the first example we were given below. What is the purpose of float a after int main()? Also could someone give another example of using a function? Thanks

#include <iostream>  
using namespace std;

float rectArea (float h, float w)
{
    float area;
    area = h * w;
    return area;
}   

int main()
{
    float a, h, w;   
    h = 3.0;   
    w = 4.0;   
    cout << "area = " << rectArea(h, w) << endl;   
    return 0;
}   
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    it is not used, so absolutely no purpose – Andrey Chernukha Mar 22 '15 at 08:27
  • @AndreyChernukha Thanks. Perfect example as to why it is so difficult to effectively learn programming from my professor. Can you recommend any websites to learn thoroughly from scratch? –  Mar 22 '15 at 08:28
  • 2
    I'm not sure about websites, I recommend you to get a C++ book and study with it – Andrey Chernukha Mar 22 '15 at 08:32
  • Why would I not just use a macro here? –  Mar 22 '15 at 08:32
  • http://stackoverflow.com/questions/9104568/macro-vs-function-in-c – Andrey Chernukha Mar 22 '15 at 08:34
  • 2
    @TaylorClark SO isn't the best resource to use to start learning a new programming language - it's much better suited to help answer some of the questions that come up once you've got the hang of it, by which point you should really know what an example of using a function looks like. As Andrey mentioned, a book is a much better place to start. – MooseBoys Mar 22 '15 at 08:39
  • @MooseBoys Thanks man. A little upset that I've paid $900 to take a course in C++ that teaches it to me in the hardest way possible. I've got a 3.5 GPA and this is the hardest class I have ever taken in my entire life but when I ask a question on here someone always explains 10x better than professor. –  Mar 22 '15 at 08:45
  • The only reason to take a beginner's programming course in University is to fulfill program requirements. Otherwise, they're pretty much useless. Try the book "C++ for Dummies". – Mateen Ulhaq Mar 22 '15 at 08:48
  • @Taylor Clark It would be better if you paid me $900.:) I would provide an individual approach to learning C++.:) – Vlad from Moscow Mar 22 '15 at 08:48
  • @Taylor Clark By the way what is the highest GPA? – Vlad from Moscow Mar 22 '15 at 08:51
  • I have a 100 average in the class and I understand what he teaches but I feel like we are not covering near as much as we should be in order to actually be able to use the language to program more than just small pointless assignments that have no use –  Mar 22 '15 at 08:58
  • @Taylor Clark I can guess that non-pointless assignments are some graphic programs. In this case you should learn C# and for example WinForms. – Vlad from Moscow Mar 22 '15 at 09:02

3 Answers3

4

type specifier float in the first line after main has nothing common with functions because this line defines scalar objects.

Statement

float a, h, w; 

defines objects a, h, and w as having type float that is these objects can store float numbers.

I think your professor means the following

int main()
{
    float a, h, w;   
    h = 3.0;   
    w = 4.0;   

    a = rectArea(h, w);

    cout << "area = " << a << endl;

    return 0;
}   

Otherwise variable a is defined but not used in the program.

The code would be more clear if he used more meaningful names. For example

int main()
{
    float area, height, width;   
    height = 3.0f;   
    width  = 4.0f;   

    area = rectArea(height, width);

    cout << "area = " << area << endl;

    return 0;
}   

What is the purpose of float a after int main()?

Shortly speaking the purpose of variable a is to store the result of calculating the area of a rectangle. In fact it is redundant if you are going simply to output the area on console. In this case you could remove the definition of a and write in the output statement

cout << "area = " << rectArea(h, w) << endl;

as it is shown in the original code.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

'a' is the variable intended to store the area of the rectangle after multiplying width * height. 'float' describes what type the variable 'a' is, and float is a decimal to 7 digits of precision. So the area of the rectangle is a decimal to 7 digits of precision.

0

The line: float a, h, w; defines 3 variables called a, h and w of type float. Variable a doesn't have a purpose in this example because it's not used.

Miro Bucko
  • 1,123
  • 1
  • 13
  • 26