1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Comprehending some C code (simple function calls)

  1. Oct 7, 2015 #1
    1. The problem statement, all variables and given/known data
    Hi, I have a piece of code that I'm trying to comprehend but I don't understand some aspects. Here is that code:

    Code (Text):

    #include <stdio.h>

    /* function prototype declaration for FindSum */

    void FindSum(int, int, int *);

    int main(void)
    {

    int a=2, b=5, c=1, x=3, y=4, z=7;

    /* body of main */

     FindSum (a, b, &c); /* a first call to FindSum */
     printf(“first call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);
     FindSum (x, y, &z); /* a second call to FindSum */
     printf(“second call in main %d %d %d %d %d %d \n”, a, b, c, x, y, z);

    return 0;

    }


    /* definition of FindSum */

    void FindSum (int a, int b, int *c)
    {

     a += (b * 2);
     b += (b * 2);
    *c += (b * 2);

     printf(“in FindSum: %d %d %d \n”, a, b, *c);

    }
     


    2. Relevant equations


    3. The attempt at a solution

    The first things I'm trying to get a grasp on:
    In the main function, where it first calls FindSum it has a b c as its parameters, and in the second call it has x y and z. Is this initial call to the function only going to edit the values of the integers for a b and c? Would it simply then shout out the values of x y and z? If this is the case, I'm assuming it would do the same for the second call to the function, but for x y and z. (And this is where it begins to confuse me)

    In the function of FindSum, it only modifies the value of a, b, and c. During the second call to the function FindSum, would it simply echo the values of x y and z? They won't be changed?

    And in the line "b += (b*2)" would it be

    a += (5 *2)
    b += (5 * 2)
    *c += (10 * 2)

    Sorry for all the questions. I'll try compiling the code when I'm at home, but for now I just want to try acting like the computer. Thanks alot!
     
  2. jcsd
  3. Oct 8, 2015 #2

    Svein

    User Avatar
    Science Advisor

    No. The first two parameters (here a and b) are called by value, which means that just a copy is sent to FindSum. The third parameter (here c) is called by reference, which means that it may be changed by FindSum.
    What exactly do you mean by that?
    Oops - inside FindSum, a, b and c refers to the parameters to the function, not to the variables with the same name in main. As I explained above, only the third parameter may be assigned to - and it is.
    Again: A copy of x and y are sent to FindSum as parameters. FindSum modifies the variable referenced by the third parameter, so z will be changed.
    Almost correct - but since b was 5 to begin with, when you add (5*2), it becomes 15 before the line with *c. Thus *c += (15*2).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Comprehending some C code (simple function calls)
  1. Simple C coding (Replies: 14)

Loading...