Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   need help understanding this please (http://www.go4expert.com/forums/help-understanding-t10734/)

C Lueless 22May2008 21:12

need help understanding this please
 
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
}

FCC 22May2008 23:42

Re: need help understanding this please
 
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.

C Lueless 23May2008 00:26

Re: need help understanding this please
 
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?

FCC 23May2008 01:12

Re: need help understanding this please
 
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.

atul.sharma12 23May2008 15:24

Re: need help understanding this please
 
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.

C Lueless 23May2008 17:38

Re: need help understanding this please
 
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?

atul.sharma12 23May2008 17:59

Re: need help understanding this please
 
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.

C Lueless 23May2008 19:43

Re: need help understanding this please
 
Thanx


All times are GMT +5.5. The time now is 09:59.