Thread: Newbie question

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie question

    Hi guys,

    When a user defined fucntion returns a value, it goes to the fuction in the main right?. And that value has to be assigned to a variable right?.
    for example:

    Code:
    void main()
    {
    ans=function();
    }
    
    
    int function()
    {
    int add;
    add=1+3;
    return add;
    }
    so if I want to use the value in the variable "ans". I would just call the function like this:
    Code:
    funtion();
    or like this
    Code:
    funtion(ans);
    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When a user defined fucntion returns a value, it goes to the fuction in the main right?
    Sort of (if my understanding of what you wrote is correct), but it doesnt have to be the main() function, it could well be another function that calls this function.

    And that value has to be assigned to a variable right?.
    Not necessarily.

    so if I want to use the value in the variable "ans". I would just call the function like this:
    Not sure what do you mean, but if a function named function() returns an int, then you could use:
    int ans = function();

    by the way, void main() should be int main(void) here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    No. If you call the function from main yes, if you call the function from another function than no.

    Also main should be int not void
    Code:
    int main(void)
    {
      return 0;
    }
    So your code should look like this

    Code:
    /* function prototype */
    int function(void);
    
    int main()
    {
      int ans = 0;
      ans=function();
      return 0;
    }
    
    int function(void)
    {
      int add;
      add = 1 + 3;
      return add;
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    thanks for the reply

    I meant that if I want the value in the varaible "ans" anywhere in the program how would I get that? Is it just if I call the funtion anywhere in the program it will give the value in the add?
    Last edited by jat421; 02-10-2005 at 02:46 AM.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Quote Originally Posted by jat421
    And that value has to be assigned to a variable right?for example:
    Code:
    void main()
    {
       ans=function();
    }
    
    int function()
    {
    int add;
    add=1+3;
    return add;
    }
    That's correct.
    so if I want to use the value in the variable "ans". I would just call the function like this:
    Code:
    funtion();
    or like this
    Code:
    funtion(ans);
    This part is not clear as to what exactly are you trying to ask. Your first part indicates that you totally understand how the functions return values and then this. Can you be specific as to what you're trying to ask?

    eDIT:
    I think I had the window open for too long.
    Last edited by rjv_rnjn; 02-10-2005 at 02:52 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Sorry if I am not clear

    ok, so what I mean is that the value returned from the funtion() is in "ans" now right. So if I want to call the funtion anywhere in the program how will I get that value from the "ans". Do I just call the funtion like this function() or will have to to write ans in the front like this funtion(ans) so it know I want the value in "ans".


    Thanks

    Edit: Don't go to sleep now, I need your help hehe :P

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Quote Originally Posted by jat421
    I meant that if I want the value in the varaible "ans" anywhere in the program how would I get that? Is it just if I call the funtion anywhere in the program it will give the value in the add?
    That depends upon the scope of the variable. If the variable is defined within main() then that can be accessed only within main() but if its defined above main() and outside any function then that variable is available everywhere in the program. For eg.
    Code:
    int main(){
      int ans;
      ans = function();
      return 1;
    }
    
    int function(){
    ...something..
    }
    Here the variable 'ans' can be accessed anywhere in the main but not in function() as its local to main().
    Code:
      int ans;
    int main(){
      ans = function();
      return 1;
    }
    
    int function(){
    ...something..
    }
    Here 'ans' can be accessed by function() as well as its a global variable.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    thanks for the reply rjv_rnjn

    But that not what I am looking for. Sorry if I am not clear. Here I will try to rearrange my question.

    ok, so what I mean is that the value returned from the funtion() is in "ans" now right. So if I want to call the funtion anywhere in the program how will I get that value from the "ans". Do I just call the funtion like this function() or will have to to write ans in the front like this funtion(ans) so it know I want the value in "ans".
    thanks

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    12
    Quote Originally Posted by jat421
    Do I just call the funtion like this function() or will have to to write ans in the front like this funtion(ans) so it know I want the value in "ans".
    If you want the value returned from the function() to be stored in "ans" then you do
    Code:
     ans = function();
    and not
    Code:
    function(ans);
    . Does that make things clear?

  10. #10
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    >Do I just call the funtion like this function() or will have to to write ans in the front like this funtion(ans) so it know I want the value in "ans".

    after the function call "ans" already has a value. so if you want to use "ans", just use the variable itself. no need for another function(). i believe someone already said this earlier but "ans" (the way you declared it) is only usable in the main().

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    thanks for the reply

    I think I don't know how to put the question . Ok here one more time lol. If I want to call the function() anywhere in other funtion called calculate() what will I write in calculate function, so I can get the value from function().

    sorry for making thing complicated.

  12. #12
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    You mean to pass the value returned by the function() to a new function called calculate()?
    Something like this, maybe?
    Code:
    #include <stdio.h>
    
    //declare functions
    int function(void);
    int calculate (int a);
    
    int main ()
    {
       int abs = function();
       int res = calculate(abs);//takes as parameter the value of function()
       
       printf ("%d", res);
       getchar();   
       return 0;
    }
    
    //define functions
    int function()
    {
       int a;
       a = 1+3;//your example
       return a;
    }   
    
    int calculate (int a)
    {
       int c;
       c = a+3;
       return c;
    }

  13. #13
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    I think you should take a look here: functions

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    yes that what I meat , lol finally thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM