| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
basics in using function
hi i tried a small program using function which is as follows
#include<stdio.h> #include<conio.h> void main() { int a=15,b=20,sum; message(); } message(int c,int b) { c=b; printf("%d",c); } the o/p of this code was the value of a i.e 15. but i wanted 2 display the value of b i.e. 20. pls tell me how and wats happening here |
|
#2
|
||||
|
||||
|
#include<stdio.h>
#include<conio.h> void main() { int a=15,b=20,sum; message(a,b); } message(int c,int b) { printf("%d",b); }
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
You should at least got some errorcodes when compiling this a C code.
The function 'message' must always be calles with parameters, as B-Con suggested: message(a, b); This way the values of a and b are passed into the function. Also a function has to be defined with a return type: void message(int a, int b) So what environment are you using? Also the output of the program would be 0 or an uninitialized value. Variables a and b are local values within the fuction main. Therefore the value of a (15) can never be shown by the printf in the function message. Basically I would suggest: #include<stdio.h> #include<conio.h> void main() { int b=20; message(b); } void message(int b_copy) { int c = b_copy; printf("%d",c); } |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > basics in using function |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|