+ 1

Why doesn't the function in the comment take input?

https://code.sololearn.com/c11rb4HIjD49/?ref=app

25th May 2019, 6:05 AM
Nitin Bhattacharyya
Nitin Bhattacharyya - avatar
7 Answers
+ 4
Because it is commented out. If you uncomment it it doesn't take input because it is not invoked. If you invoke it, it will take input but there is no code to display these values in the function and the structure is not accessible outside the function.
25th May 2019, 6:24 AM
Sonic
Sonic - avatar
+ 3
Nitin Bhattacharyya I have already given the answers from the second sentence onwards in my previous post.
25th May 2019, 6:29 AM
Sonic
Sonic - avatar
+ 3
Nitin Bhattacharyya no, pass by value won't work. You will need to pass the address of the struct to the input function and use a pointer in the signature of this function.
25th May 2019, 6:55 AM
Sonic
Sonic - avatar
+ 1
Everything works: Name; emp_id; age.
25th May 2019, 6:13 AM
Solo
Solo - avatar
+ 1
Sonic no I tried using the function and I wasn't getting output,that's why I commented it out to show the actual function that runs,I want to know why the function in the comment doesn't work(not because I commented it)
25th May 2019, 6:27 AM
Nitin Bhattacharyya
Nitin Bhattacharyya - avatar
+ 1
Sonic i understand now in the void function ,if I invoke it it was indeed taking input but that structure variable wasn't accessible by the other functions,and I also declared a variable e in the main function and put that in display function. Now this e cannot access the data from the void input function,and since I am putting the e from main function in the display function,I don't get the desired output,is it right??so will it work if I write void input(struct Employee e) and invoke the function input (e)?
25th May 2019, 6:49 AM
Nitin Bhattacharyya
Nitin Bhattacharyya - avatar
0
Sonic thanks for helping with the problem,also it seems if I declare a global variable e and then use void input() { gets(e.name); scanf("%d",&e.emp_id); scanf("%d",&e.age) } Void display() {puts(e.name); printf("%d",e.emp_id); printf("%d",age); } And now if I don't declare a variable of employee type again in main and invoke the functions it works. Is it because since there is only one variable declaration,and no variable is declared inside the functions,all the values get stored in that variable e and since it's global it's accessible by all the functions?sorry for having too many doubts,I am teaching myself to code and I like dive into the concepts of how things work.
25th May 2019, 7:08 AM
Nitin Bhattacharyya
Nitin Bhattacharyya - avatar