View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
Copy Variable |
Posted: Sat Mar 17, 2012 2:18 am |
|
|
How to insert a variable from one function to another?
For example:
Code: |
func(1){
var_name=10;
var_name+=10;
printf("var_name = %d", var_name);
}
void main(){
unsigned int8 var_name;
while (1){
func(1);
}
}
|
How could I print variable declared in main function, to another one?
Thx in advance! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Mar 17, 2012 2:44 am |
|
|
Basic C. I'd suggest you get a C primer....
Code: |
void func(unsigned int8 ival){
//This says 'func', is a function returning nothing (hence 'void', but
//expecting to receive an unsigned int8, which it will refer to internally
//as ival
ival+=10; //Add ten to the value passed
printf("main val+10 = %d\n\r", ival);
}
void main(void){
unsigned int8 int_var; //Call your variables with names that help
//you remember 'what' they can hold
int_var=20;
while (1){
func(int_var);
}
}
|
Which will print "main val+10 = 30" taking the value passed _to_ it from main, adding ten, and then printing it.
You need to read a basic book on C, since you can then return a value as well (which can be different), or use global variables (study the dangers of these). This is basic 'programming 101' stuff, not really what this forum is for.
Best Wishes |
|
|
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Sat Mar 17, 2012 4:48 am |
|
|
If you give me, some links with this book would be great! |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Mar 17, 2012 9:40 am |
|
|
Seriously, by a primer, I meant a primer. The C programming language, is pretty much written for programmers, and though the book everyone should have, I really would suggest starting with a basic C primer. A Google search will find dozens.
Best Wishes |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Sat Mar 17, 2012 11:31 am |
|
|
While K&R "The C Programming Language" is the "bible", it can also cure insomnia Browse through the books at places like Amazon.com and look at the user comments - what works for one person may not for another, but there are some excellent intro books out there - definitely worth the investment. _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
|