C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 15th, 2005, 09:01 AM
roogie roogie is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 2 roogie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 47 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old August 15th, 2005, 04:50 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 1 m 43 sec
Reputation Power: 4
#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.



Reply With Quote
  #3  
Old August 16th, 2005, 05:20 AM
MichaelSoft MichaelSoft is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: The Netherlands
Posts: 121 MichaelSoft User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 20 sec
Reputation Power: 4
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);
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > basics in using function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway