Issues learning how to call a function

This is a discussion on Issues learning how to call a function within the C++ Programming forums, part of the General Programming Boards category; My professor has given an assignment where the objective is to learn how to call a function. The assignment is ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19

    Issues learning how to call a function

    My professor has given an assignment where the objective is to learn how to call a function. The assignment is a program that calculates the future value of a savings account. We are supposed to create a function that calculates the interest value based on inputs from the user. I have created a program that calls a function, but I can't seem to get the function to return anything at all. I'm having trouble assigning a value to variables in the function, and I do not know how to take values inputted by the user in the int main() function and use them in the function that I have created. My professor asked that we introduce the function with a prototype above the int main() function, and put the actual commands for the function below the main. Here is what i have so far, how can I 1. get the function to return anything. 2. use variables inputted by the user in the main in my function, 3. assign values to the variables in the function. Thanks
    Code:
    #include <iostream>
    using namespace std;
    double futureinvestmentvalue (double initialdeposit, double monthlyinterestrate, int years);
    int main () {
        double initialdeposit, monthlyinterestrate;
        int years;
        cout <<"This program calculates the future balance of your savings account.\n";
        cout <<"Please input the amount of the initial deposit into the account: ";
        cin >>initialdeposit;
        cout <<"Please input the annual interest rate of the account: ";
        cin >>monthlyinterestrate;
        cout <<"Please input the amount of time in years the account will be active: ";
        cin >>years;
        cout<<"The final balance of the account is " <<futureinvestmentvalue<< endl;
        return 0;
    }
    double futureinvestmentvalue (double initialdeposit, double monthlyinterestrate, int years){
    return 0;
    }
    /* ----program output-----
    
    This program calculates the future balance of your savings account.
    Please input the amount of the initial deposit into the account: 5
    Please input the annual interest rate of the account: 5
    Please input the amount of time in years the account will be active: 5
    The final balance of the account is 008F11AE
    Press any key to continue . . .
    
    --------------------------*/

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    OK, so
    1) Do you know how to define a function?
    2) Do you know how to call a function?
    3) Do you know how to return a value from a function?

    If you do know any of the above, can you demonstrate it by creating some elementary function, calling it and returning a value from it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Quote Originally Posted by Elysia View Post
    OK, so
    1) Do you know how to define a function?
    2) Do you know how to call a function?
    3) Do you know how to return a value from a function?

    If you do know any of the above, can you demonstrate it by creating some elementary function, calling it and returning a value from it?
    Hi, thanks for your response,

    I am a raw beginner, this is my 3rd assignment, and first time dealing with defining my own function. In this program, I tried to assign the variable futureinvestmentvalue a value of 2, just seeing if that is what the last Cout statement would return, and I received compiler errors relating to the operator "=". For your second question, as far as I understand it, I believe I successfully called the function in my program, I just can't make the function do anything. 3rd, I do not know how to return a value to a function. Thanks again.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Can you show what you've tried? We can't exactly read minds, so if we can't see it, we can't tell what's wrong.
    Make an example function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Well you defined the function correctly.

    To call a function, you write the function name, followed by the arguments you are passing to the function for it to use in a comma separated parenthesis enclosed list, in the way specified by the prototype and definition.

    To return something from a function, instead of writing return 0, write return followed by whatever you want to return.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    For example, If I replace the function at the end of my program with this:
    Code:
    double futureinvestmentvalue (double initialdeposit, double monthlyinterestrate, int years){
    futureinvestmentvalue=(initialdeposit*monthlyinterestrate);
    }
    I get a compiler error relating to the '=' operator. I need the function I am creating to take the values given to variables in the int main() and use them in the function. So for this function above, I would need the initialdeposit variable to use whatever value was given to it by the cin>> statement in the main function. I'll be away from the computer for a while, and won't be able to reply until later today. I appreciate all of your help with this.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Replace "futureinvestmentvalue=" with "return."
    To return something, don't assign to the function, use the return keyword.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Thanks for helping with that. I am still getting an error where the function is returning an incorrect value. I created this program to show the error I am getting.
    Code:
    #include <iostream>
    using namespace std;
    int addition(int a, int b);
    int main(){
    	int a,b;
    	cout<<"Input1: ";
    		cin>>a;
    		cout<<"input2: ";
    		cin>>b;
    		cout<<"Input1 plus input2 equals: "<<addition<<endl;
    		return 0;
    }
    int addition(int a, int b){
    	return (a+b);
    }
    
    
    /* -----program output-----
    
    Input1: 5
    input2: 3
    Input1 plus input2 equals: 0032104B
    Press any key to continue . . .
    
    --------------*/
    How can I get the function to perform a calculation based on the user's input instead of it returning this error?

    Thanks again folks

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    As I said above: "To call a function, you write the function name, followed by the arguments you are passing to the function for it to use in a comma separated parenthesis enclosed list, in the way specified by the prototype and definition."

    Code:
    cout<<"Input1 plus input2 equals: "<<addition(a,b)<<endl;
    You could also do:
    Code:
    cout<<"Input1 plus input2 equals: "<<addition(b,a)<<endl;
    or:
    Code:
    cout<<"Input1 plus input2 equals: "<<addition(b,53)<<endl;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Location
    Los Gatos, California, United States
    Posts
    19
    Quote Originally Posted by King Mir View Post
    As I said above: "To call a function, you write the function name, followed by the arguments you are passing to the function for it to use in a comma separated parenthesis enclosed list, in the way specified by the prototype and definition."

    Code:
    cout<<"Input1 plus input2 equals: "<<addition(a,b)<<endl;
    You could also do:
    Code:
    cout<<"Input1 plus input2 equals: "<<addition(b,a)<<endl;
    or:
    Code:
    cout<<"Input1 plus input2 equals: "<<addition(b,53)<<endl;
    Oh, I missed your first post, can't believe I did that. This worked, Thank you both for your help with my issue. I really appreciate it.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Actually, it wasn't returning an error. It was returning the address of the function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  2. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  3. Replies: 5
    Last Post: 10-17-2006, 08:54 AM
  4. System() call issues
    By vishalbhingarka in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 09:55 AM
  5. Replies: 2
    Last Post: 06-21-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21