19

I am writing a simple program that uses functions found in different .cpp files. All of my prototypes are contained in a header file. I pass some of the functions into other functions and am not sure if I am doing it correctly. The error I get is "'functionname' cannot be used as a function". The function it says cannot be used is the growthRate function and the estimatedPopulation function. The data comes in through an input function (which I do think is working).

Thanks!

header file:

#ifndef header_h
#define header_h

#include <iostream>
#include <iomanip>
#include <cstdlib>


using namespace std;

//prototypes
void extern input(int&, float&, float&, int&);
float extern growthRate (float, float);
int extern estimatedPopulation (int, float);
void extern output (int);
void extern myLabel(const char *, const char *);

#endif

growthRate function:

 #include "header.h"

float growthRate (float birthRate, float deathRate, float growthrt)     
{    
    growthrt = ((birthRate) - (deathRate))
    return growthrt;   
}

estimatedPopulation function:

    #include "header.h"

int estimatedPopulation (int currentPopulation, float growthrt)
{
    return ((currentPopulation) + (currentPopulation) * (growthrt / 100);
}

main:

#include "header.h"

int main ()
{
    float birthRate, deathRate, growthRate;
    char response; 
    int currentPopulation, years, estimatedPopulation;

    do //main loop
    {  
        input (currentPopulation, birthRate, deathRate, years);
        growthRate (birthRate, deathRate, growthrt);

        estimatedPopulation (currentPopulation, growthrt);
        output (estimatedPopulation (currentPopulation, growthrt));
        cout << "\n Would you like another population estimation? (y,n) ";
        cin >> response;
    }          
    while (response == 'Y' || response == 'y');

    myLabel ("5-19", "12/09/2010");   

    system ("Pause");

    return 0;
}    
Alex
  • 619
  • 1
  • 8
  • 25
darko
  • 2,438
  • 8
  • 42
  • 54

6 Answers6

49

You are using growthRate both as a variable name and a function name. The variable hides the function, and then you are trying to use the variable as if it was the function - that is not valid.

Rename the local variable.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235
1
#include "header.h"

int estimatedPopulation (int currentPopulation, float growthRate)
{
    return currentPopulation + currentPopulation * growthRate  / 100;
}
khachik
  • 28,112
  • 9
  • 59
  • 94
0

Your compiler is right. You can't use the growthRate variable you declared in main as a function.

Maybe you should pick different names for your variables so they don't override function names?

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
0

This line is the problem:

int estimatedPopulation (int currentPopulation,
                         float growthRate (birthRate, deathRate))

Make it:

int estimatedPopulation (int currentPopulation, float birthRate, float deathRate)

instead and invoke the function with three arguments like

estimatePopulation( currentPopulation, birthRate, deathRate );

OR declare it with two arguments like:

int estimatedPopulation (int currentPopulation, float growthrt ) { ... }

and call it as

estimatedPopulation( currentPopulation, growthRate (birthRate, deathRate));

Edit:

Probably more important here - C++ (and C) names have scope. You can have two things named the same but not at the same time. In your particular case your grouthRate variable in the main() hides the function with the same name. So within main() you can only access grouthRate as float. On the other hand, outside of the main() you can only access that name as a function, since that automatic variable is only visible within the scope of main().

Just hope I didn't confuse you further :)

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
  • This is not correct, I think. Look at main where the `estimate...` called. – khachik Dec 10 '10 at 19:45
  • Maybe, hard to tell which line is the "correct" intention here. Revised anyway. – Nikolai Fetissov Dec 10 '10 at 20:00
  • I cant consolidate it into one function because i they both have to return a value: growthrate and estimated population. To do the second option do i need to have the return value of growthrate be growthrt? – darko Dec 10 '10 at 20:07
  • @Matt, just avoid naming functions and variables with the same exact words. Adopt some convention like - function names are in **camelBackNotation**, but variables have **underscore_separated_names**, or whatever makes it clear. – Nikolai Fetissov Dec 10 '10 at 20:14
  • anyway to post code in these reply comments so I can show you my changes? – darko Dec 10 '10 at 20:21
  • Alright, edited growthrate and estimatedPopulation functions, and main. Still getting the same error. – darko Dec 10 '10 at 20:35
  • @Matt, wrong - C and C++ functions **copy** their parameters, so `var` in `int func( int var ) { ... }` is a **local variable** in that function with the value you gave it when you invoked the function. Further, a function *returns* value via `return` statement. You have to assign it like `int a = func( b, c );` or use it in expression like `int a = 12 + func( b, c );`. – Nikolai Fetissov Dec 10 '10 at 20:44
0

You can't pass a function as a parameter. Simply remove it from estimatedPopulation() and replace it with 'float growthRate'. use this in your calculation instead of calling the function:

int estimatedPopulation (int currentPopulation, float growthRate)
{
    return (currentPopulation + currentPopulation * growthRate / 100);
}

and call it as:

int foo = estimatedPopulation (currentPopulation, growthRate (birthRate, deathRate));
BlackBear
  • 22,411
  • 10
  • 48
  • 86
0

Modify your estimated population function to take a growth argument of type float. Then you can call the growthRate function with your birthRate and deathRate and use the return value as the input for grown into estimatedPopulation.

float growthRate (float birthRate, float deathRate)     
{    
    return ((birthRate) - (deathRate));    
}

int estimatedPopulation (int currentPopulation, float growth)
{
    return ((currentPopulation) + (currentPopulation) * (growth / 100);
}

// main.cpp
int currentPopulation = 100;
int births = 50;
int deaths = 25;
int population = estimatedPopulation(currentPopulation, growthRate(births, deaths));
Zac Howland
  • 15,777
  • 1
  • 26
  • 42