Why x and y occur twice | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why x and y occur twice

#include <stdio.h> int sum_up (int x, int y); int main() { int x, y, result; x = 3; y = 12; result = sum_up(x, y); printf("%d + %d = %d", x, y, result); return 0; } int sum_up (int x, int y) { x += y; return(x); } Why x and y occur twice? I mean one before main() an the other after it. how do we know which one is being called? thank you in advance ^^

15th Apr 2020, 12:07 AM
Zhengrong Yan
Zhengrong Yan - avatar
2 Answers
+ 4
In wich and every function, you need to define variables explicitly , therefore in main () x and y is defined for 1st time and. In function. sum_up() it is defined for 2nd time...
15th Apr 2020, 12:27 AM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar
+ 4
These variables defined under any specific function are called local variables and to avoid this situation you can have Global variables..........
15th Apr 2020, 12:29 AM
Raj Kalash Tiwari
Raj Kalash Tiwari - avatar