need help understanding this please

Discussion in 'C' started by C Lueless, May 22, 2008.

  1. C Lueless

    C Lueless New Member

    Joined:
    May 22, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to see if I am understanding this right for a homework assignment. Can anyone please help.

    void fun ( int first. int second) { //If I am right we are declaring variables
    first += first; // addidtion assignment what does it mean??
    second += second // same question
    ]

    void main() { //is this creating an array?
    int list [2] = {1,3}; // creates a list named 1 and 3 or is it the values of the list???
    fun(list[0], list[1]); //this calls fun
    }
     
  2. FCC

    FCC New Member

    Joined:
    May 22, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am pretty new, but I might be able to proivde some answers

    - void fun is a function you creating. It takes in two parameters of the type int, which you called first and second. They can called anything, even the same names as variables in the main program.

    - first += first this means you are incrementing the first variable by 1 and then assigning it back to first. So like if it was 4 increment it to 5, and then assign it back to first. So in the end, first has the value 5.

    - void main is the initial function called by the program. You need this because the program has to know where to start

    - int list [2] = {1,3} is creating like an int array of size 2. So you have 2 cells. And the first cell has 1, and the second has 3.

    fun(list[0], list[1]) calls fun as you stated. and sends in the 1 cell, and the 2 cell as input to the function.

    hope that helps.
     
  3. C Lueless

    C Lueless New Member

    Joined:
    May 22, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thanx!!!!

    Now one more question what does fun do??

    I understand the incrementing part of first =+ first.

    the last line reads

    fun(list[0], list[1]); // fun (1*3)

    How does Fun take part?
     
  4. FCC

    FCC New Member

    Joined:
    May 22, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    well you are calling the function fun. you can't do the incrementing first and second stuf without calling fun.

    Think of it this way., The program starts with the function main..and it runs some code and then sees the line fun(...). Then it goes...ok I need to run the function fun. So it leaves the main function and enters the fun function and then runs the code in there (ie. the incrementing) code. And then it returns back to the main function and continues until there is nothing left to run.
     
  5. atul.sharma12

    atul.sharma12 New Member

    Joined:
    May 21, 2008
    Messages:
    15
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg.
    Location:
    This Planet only!!!!
    From the Main you are calling the function fun and parameters are being passed by value. Fun is returning void so what ever the value you are passing to the function Fun that will be incremented by one and the scope of those values will be in the Fun.Incremented values will not be reflected in the main. To achieve that you need to pass the parameters by reference.
     
  6. C Lueless

    C Lueless New Member

    Joined:
    May 22, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    so, if they are being passed br reference, the values will be incremented to 2 and 4. If they are passed by value would it be 1 and 1?
     
  7. atul.sharma12

    atul.sharma12 New Member

    Joined:
    May 21, 2008
    Messages:
    15
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    S/W Engg.
    Location:
    This Planet only!!!!
    If the parameters are being passed by reference then the modifications done will be reflected
    in the original parameters also.And If they being passed by value then the copy of the variable is passed to the function and the scope of those passed value will be within the function itself.Once out from the function they are gone.

    So in your case if the parameters are being passed as value then withing the function the value will be 2 and 4.But once the control return to the main then in the main it will be 1 and 3.

    if the parameters are being passed as reference then the value will be 2 and 4 in the main also.
     
    shabbir likes this.
  8. C Lueless

    C Lueless New Member

    Joined:
    May 22, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice