if i have function (A) contains many function & i want to make to another .........

Status
Not open for further replies.

mahm150

Full Member level 1
Joined
Dec 14, 2010
Messages
98
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Cairo, Egypt, Egypt
Activity points
2,001
if i have function (A) contains many function & i want to make to another .........

if i have function (A) contains many functions & i want to make to another function like function (A) contains many function but with different variables in each function is there any method to minimize the code using structure

---------- Post added at 16:24 ---------- Previous post was at 16:22 ----------

the second function do the same job of function(A) but with different in variable name
 

Re: if i have function (A) contains many function & i want to make to another ......

I wonder, if you read your own question can you understand what you are asking because I certainly can't...
 

Re: if i have function (A) contains many function & i want to make to another ......

:smile: sorry if cant discuss clearly
I want to say that i have functionA()
void functionA(void)
{
functionx(void);
functiony(void);//
functionz(void);
}
and i want do another function do the same job as functionA() but deal with another varaibles the different only in name
void functionB(void)
{ // this function do the same as functionA()
functionX(void);
functionY(void);//
functionZ(void);
}
the question is that is there any method to make one function deal with the different variables in functionA() and functionB() using structure?
 
Last edited:

Re: if i have function (A) contains many function & i want to make to another ......

copy paste function A and rename it is functionB and change all variables... dont forget to declare them ......
 

Re: if i have function (A) contains many function & i want to make to another ......


what variables, I don't see any variables in your code , all functions use void so where are these variables used?
 

Re: if i have function (A) contains many function & i want to make to another ......

in my case variable is global i make some manipulation on it i will post to you one of my function
###########################################################
void edate_menu(void)
{
unsigned char prev_edate;
prev_edate=edate;
lcd_gotoxy(0,1);
lcd_putsf("date=");
lcd_gotoxy(5,1);
itoa(edate,string);
lcd_puts (string);
while(MENU==1 && CANCEL==1)
{
if(UP==0)
{
while(UP==0);
edate+=1;
if(edate==32)
{
lcd_gotoxy(0,1);
lcd_putsf("date= ");
edate=1;
}
lcd_gotoxy(5,1);
itoa(edate,string);
lcd_puts (string);

}
if(DOWN==0)
{
while(DOWN==0);
edate-=1;
if(edate==0)
{
lcd_gotoxy(0,1);
lcd_putsf("date= ");
edate=31 ;
}
lcd_gotoxy(5,1);
itoa(edate,string);
lcd_puts (string);
}
}
if(MENU==0)
{
// while(MENU==0);
SAVED_message() ;
EEprom_WRite(edate,edate_address);
// menu_index++;
sdate= EEprom_READ(edate_address);
long_delay();
}
else
{
//while(CANCEL==0);
NOT_SAVED_message();
EEprom_WRite(prev_edate,edate_address);
// menu_index++;
long_delay();
}
}
#########################################################
 

Re: if i have function (A) contains many function & i want to make to another ......

I'm not sure if this is what you mean but you can declare a structure

Code C - [expand]
1
2
3
4
struct structure_tag_name {type member_1; type member_2; type member_3;}
 
//and then two structure variables and use then in your code
structure_tag_name my_var1, my_var2;



as an example

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
struct my_structure {char my_char; int my_int; char my_array[10]}
 
my_structure structure1, structure2;
 
then you can use 
structure1.my_char  // char variable
structure1.my_int   // integer variable
structure1.my_array  // array of 10 char
 
structure2.my_char
structure2.my_int
structure2.my_array



is this what you ask?

---------- Post added at 20:05 ---------- Previous post was at 19:58 ----------

if I understand correctly functionx(void); functiony(void);// functionz(void); are the same as functionX(void); functionY(void);// functionZ(void);
in that case instead of writing the same functions two times you can use one function (one x, one y, one z) and send some variables when you call them or send a flag and use a condition inside the function to modify different variables
 
Re: if i have function (A) contains many function & i want to make to another ......

the second solution is ok and i can do it but i need to know how to use structure with function as you discuss structure first but how can i use it with function ,
can i use the members of structure is the functionX(void); functionY(void);// functionZ(void); or not

---------- Post added at 19:38 ---------- Previous post was at 19:32 ----------

and is the solution of structure is accepted or not if accepted i think it minimize my code
 

Re: if i have function (A) contains many function & i want to make to another ......

I think you can use pointer to functions but I haven't used that, the idea is like

Code:
Pointers to functions
-------------------------------------------------------------
Using a pointer to a function allows for functions to be called from the result of lookup operations, or passed by reference to other functions.
	void task1(void) { printf(“task1\n”); }
	void task2(void) { printf(“task2\n”); }
	void (*task_list[2]) (void) = {task1, task2};		// an array of pointers to functions
	void (*fp) (void);					// fp is a pointer to a function
	fp = task[1];						// assign the function address to fp
	(*fp)();							// execute the function pointed to by fp


Another solution I would recommend is the use of pointers, like this

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
void functionxX (char *input1, char *input2,char *out1, char *out2 )
 
{
    *out1=*in1;
    *out2=*in2;
 
}
 
 
you call it like
 
functionxX (&in1, &in2, &out1, &out2); // this in place of functionx
functionxX (&in3, &in4, &out3, &out4); // this in place of functionX


as you can see you only need one function that uses values based on the pointers it receives

Alex
 
Re: if i have function (A) contains many function & i want to make to another ......

void functionxX (char *input1, char *input2,char *out1, char *out2 )

{
*out1=*in1;
*out2=*in2;

}
functionxX (&in1, &in2, &out1, &out2); // this in place of functionx
functionxX (&in3, &in4, &out3, &out4); // this in place of functionX
*****************************************************************
if i understand that it one function called by different parameter but why you use pointer with it
first solution not clear for me
 

Re: if i have function (A) contains many function & i want to make to another ......

If you don't want to use pointers then you can use global variables but in that case you have to use a flag and depending on the value it has then write and read different global variables.

You said that functionx and functionX were exactly same function that were just reading/writing different variables and you wanted to shrink the code, in that case the pointers as used above will make the function work with different variables depending on the pointer addresses without writing any additional conditions inside.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…