Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Using parameters of a function outside a function

Status
Not open for further replies.

NewbeeAVR

Junior Member level 2
Joined
Dec 2, 2017
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
195
I need to write a function relating to LCD as follows

Code:
void init(uint8_t rs, uint8_t en);

and

Code:
toggle_en();

I need to use parameter 'en' within init() function in toggle_en() function how to do that
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,731
Helped
5,069
Reputation
10,163
Reaction score
4,924
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
133,222
If I understand the question, the parameters are passed in the same order as you write them.
"void init(uint8_t rs, uint8_t en);" is a function prototype, not a function in itself. It warns the compiler that "init" expects two uint8_t data types when it is used later.

Within the real "init" function declaration you should place two uint8_t variables and these will be given (passed) the values used when you call the function and in the order they were given. A copy of the parameters is used, not the originals in this instance so 'rs' and 'en' still hold the same values after the function returns and they can be used again by another function. Inside the function, private copies of the parameters are used and the copies are lost when the function ends (returns).

Brian.
 

NewbeeAVR

Junior Member level 2
Joined
Dec 2, 2017
Messages
21
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
195
Thank you for your reply. I mean can I make something like this

Code:
void init(uint8_t rs, uint8_t en){
static _en = en;
static _rs = rs;
}
void toggle_en(){
//make use of _en here
//_rs is used in some other routine
}
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
23,047
Helped
4,716
Reputation
9,448
Reaction score
5,092
Trophy points
1,393
Activity points
152,679
Hi,

only guessing:
EN and RS are typical signals for a HD44100 compatible parallel LCD interface.

Thus my question: do you expect EN value to be something like "PORTA,1"?

Klaus
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
15,731
Helped
5,069
Reputation
10,163
Reaction score
4,924
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
133,222
It doesn't look like a good way to do it.

When you make a variable 'static' it means it's value is preserved when the function ends and is restored when the same function is called again. It doesn't make the value available to another function though, it gets stored in a special part of RAM reserved for use by that function and not easily accessible to other functions.

If you want to change a variable in one function and make it available to other functions, the easiest way is to return the value when the function ends. To do that, change "void init" (void means it DOESN'T return a value) to "uint8_t init" which tells the compiler it will return a uint8_t type of variable. At the end of the function, instead of just closing the braces, add the line "return <value name>;". The variable will then be available after the function finishes and you can use it like this:
<new variable> = init(parameters);

If you do it that way, remember to change the function prototype as well or the compiler will tell you there is an error in definitions. So in your code:
void init(uint8_t rs, uint8_t en);
would become:
uint8_t init(uint8_t rs, uint8_t en);

Brian.
 

andre_luis

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,479
Helped
1,183
Reputation
2,385
Reaction score
1,181
Trophy points
1,403
Location
Brazil
Activity points
54,963
Using parameters of a function outside a function

In general, making access to resources of the lower layers of the protocol (eg. electric on this case) from outside the functions designed to doing so, it is not a good practice. While you are in a non-object-oriented programming environment and/or do not use semaphores signals to assure exclusive access to prevent conflicts, you will not notice any impediment to implement as you wish, but as the program grows in complexity, it will be in the future one of those vulnerabilities prone to be the root of problems, soon or later.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top