+ 1

A question regarding functions in c

# include <stdio.h> void fun(int x) { x = 80; } int main() { int y = 50; fun(y); printf("%d", y); return 0; } when executed it prints 50, i thought it would print 80.. why its printing 50?

2nd Mar 2018, 8:27 AM
Ayush walia
Ayush walia - avatar
5 Answers
+ 1
In C you can pass arguments only by value.... This mean that you cannot modify "original" variable value inside a function: void not_modify(int i){ i= 5; } .... int my_int= 10; not_modify(i); // here my int is always 5 How modify variable from inside functions? Simple... Pass it adress (always by value) hence pointers help you.... This allow to modify a variable inside a function: void modify(int* i){ *i= 5; } ... int my_var= 10; modify(&my_var); // here we pass adress var // here my_var value is 5
2nd Mar 2018, 10:30 AM
KrOW
KrOW - avatar
+ 4
Sorry my fault, I should have explained better, you are missing this line: int y = 50; y = fun(y); // <--- And what it is doing, is setting the returned value to your y variable
2nd Mar 2018, 10:19 AM
Tarantino
Tarantino - avatar
+ 2
Hey, I have never studied C but from what I can see the function (fun) just sets 80 to the parameter x, nothing is returned. When you pass 50 to the function,yes that now becomes 80 but because it is not returning anything, the change has no effect on the y, so now when you print y, it is printing the unchanged value of the local variable.
2nd Mar 2018, 9:29 AM
Tarantino
Tarantino - avatar
0
Ok, i tried this. # include <stdio.h> int fun(int x) { x = 80; return x; } int main() { int y = 50; fun(y); printf("%d", y); return 0; } Guess what.. answer is till 50... ;_; why..
2nd Mar 2018, 10:11 AM
Ayush walia
Ayush walia - avatar
0
Hi Ayush, I tried running your program with a few modifications in an effort to see what happens when it runs (ie. debugging): #include <iostream> #include <stdio.h> using namespace std; int fun(int x) { x = 80; cout << "inside fun function def and x is equal to " << x << endl; return x; } int main() { int y = 50; //fun(y); cout << "inside main() and fun(y) is equal to "<< fun(y) << endl; cout << "printf(\"%d\", y) displays the following: " << endl; printf("%d", y); return 0; } If you copy paste the above you should get the following output: inside fun function def and x is equal to 80 inside main() and fun(y) is equal to 80 printf("%d", y) displays the following: 50 Let me explain first what i did. Rule number 1: when we don't know what's wrong we start displaying on screen what we have by using "cout" keyword. As you can see I utilized in every possible calc in order to trace what your code does. And according to the output it is highly probable the printf() is the culprit. I checked online that this is a function that was primarily used in C, but the role for C++ has been taken by the "cout" keyword. So the question here is: what was your motive for using printf()? What were you trying to accomplish? Was it something more perplexed or a simple cout should suffice? Hope this helps!
4th Aug 2020, 11:48 PM
Mina Coutsoucos
Mina Coutsoucos - avatar