Thread: Passing Values between Functions

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    69

    Passing Values between Functions

    Hello,

    In the below code after returned the value of "d", is it goes to int calsum (int x, int y, int z); or sum = calsum (a, b, c); .

    I have a lot of confusion to understand which steps code will takes after value returned from the return statement. Please explain.

    Code:
    #include <stdio.h>
    int calsum (int x, int y, int z);
    int main()
    {
    int a, b, c, sum;
    printf ("Enter any three numbers");
    scanf ("%d %d %d", &a, &b, &c);
    printf ("Sum = %d\n", sum);
    return 0;
    }
    int calsum (int x, int y, int z)
    {
    int d;
    d=x+y+z;
    return (d);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You did not actually call the calsum function.
    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
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Code:
    #include <stdio.h>
    int calsum (int x, int y, int z);
    int main(void)
    {
    int a, b, c;
    
    printf ("Enter any three numbers");
    scanf ("%d %d %d", &a, &b, &c);
    printf ("Sum = %d\n", calsum(x,y,z));
    
    return 0;
    }
    int calsum (int x, int y, int z)
    {
    return x+y+z;
    }
    As laserlight said you did not call calsum. But now you call it. In addition you need no d variable only the return statement to return your sum.The result of the function will be printed from printf function and that's all.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Code:
     int calsum(int x, int y , int z);
    is only a declaration of the funtion in order to help the compiler to check the call later.

    The values goes to the call. After the call the value of the function is returned to the caller function if it has a value and return type of course. In other case the function will do the calculation (whatever it is) and the flow of the program will be continued to the point of the call.The program will continue to the next statement after this call.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    69
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing in values to functions
    By Mentallic in forum C Programming
    Replies: 7
    Last Post: 10-11-2012, 01:34 AM
  2. Passing Values From Functions
    By skmightymouse in forum C Programming
    Replies: 21
    Last Post: 03-25-2012, 11:48 AM
  3. Problem Passing Values between functions
    By jamez05 in forum C++ Programming
    Replies: 2
    Last Post: 05-02-2007, 01:21 PM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 08:28 PM