# how can i do a seperate function thats out of the main function?

#### rocker123uk

Joined Dec 6, 2015
33
hey guys,
thank you for reading my thread, i am creating a program where a stepper motor does 1 step and reads sensor values, however, to read the sensor values i have to always do the following code:

char *temp = "0000";
int a=0;
ADCON0 = 0b10010000; //AN2 analog channel selected
ADCON1 = 0b01001001; //PORT configuratoin to AN2
TRISA = 0b00000100; //RA2 analogue input
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
do {
temp [0] = adc_value/1000 +48 ; //add 48 to get the ascii value
temp [1] = (adc_value/100) %10 +48 ;
temp [2] = (adc_value/10) %10 +48;
Lcd_Out(1,11,temp);
Delay_ms(100);

Lcd_Out(2,1,"Too hard ");

the above is quite long as i have to copy and paste the highlighted part again and again in the main code, i have tried doing it in a separate function, however, as i try to read the adc_value in the main function, it says that the function is undeclared (i know i have to copy the declaration but that would be the same thing all over), any suggestions would be appreciated, of if theres a better way...Thanks guys i just want to save the time of copying all that again and again and saving the hedache of checking incase there is a fault or change needed in the future

#### WBahn

Joined Mar 31, 2012
26,434
It might, possibly, be helpful if you told people what language you are using. Sure, we can guess that it is probably some variant of C, but engineering is not about guessing.

You can't call a function before it is declared (declared and defined are two different things -- and that's relevant here). But once it is declared, either via the definition or a prototype, you can call it until the cows come home.

#### spinnaker

Joined Oct 29, 2009
7,835
hey guys,
thank you for reading my thread, i am creating a program where a stepper motor does 1 step and reads sensor values, however, to read the sensor values i have to always do the following code:

char *temp = "0000";
int a=0;
ADCON0 = 0b10010000; //AN2 analog channel selected
ADCON1 = 0b01001001; //PORT configuratoin to AN2
TRISA = 0b00000100; //RA2 analogue input
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
do {
temp [0] = adc_value/1000 +48 ; //add 48 to get the ascii value
temp [1] = (adc_value/100) %10 +48 ;
temp [2] = (adc_value/10) %10 +48;
Lcd_Out(1,11,temp);
Delay_ms(100);

Lcd_Out(2,1,"Too hard ");

the above is quite long as i have to copy and paste the highlighted part again and again in the main code, i have tried doing it in a separate function, however, as i try to read the adc_value in the main function, it says that the function is undeclared (i know i have to copy the declaration but that would be the same thing all over), any suggestions would be appreciated, of if theres a better way...Thanks guys i just want to save the time of copying all that again and again and saving the hedache of checking incase there is a fault or change needed in the future
Have you heard of a period at the end of a sentence and a capital letter at the start of a sentence? You post is VERY hard to read.

What function is undeclared?

#### mcgyvr

Joined Oct 15, 2009
5,394
Code:
//Declare all functions used in this code
void setup();
void loop();
void stepmotor();

//declare global variables.

void setup() {
// put your setup code here, to run once:

}

void loop() { // put your main code here, to run repeatedly:
stepmotor(); //go the the stepmotor function

}

//put sensor read code here.. it will return back into the loop when done
}

void stepmotor() {
//code to turn motor based on the sensor value goes here
}

Last edited:

#### vpoko

Joined Jan 5, 2012
267
mcgyvr's answer pretty much covered it, but in C you have to declare a function (using just the function signature) separately from defining it (where you actually specify the code in the function body).

So if you have a function that you define as:
Code:
void doSomething(int n)
{
/* some code */
}
Then at the top of your program (before any function definitions, often done in a header file but can be in your c file) you should have:
Code:
void doSomething(int n);
See http://www.tutorialspoint.com/ansi_c/c_using_functions.htm for a more detailed explanation.

Last edited:

#### WBahn

Joined Mar 31, 2012
26,434
mcgyvr's answer pretty much covered it, but in C you have to declare a function (using just the function signature) separately from defining it (where you actually specify the code in the function body).
No, you do not have to declare a function separately from its definition. I almost never declare function prototypes except in header files for functions that I want to use in files other than the ones in which they are defined. I subscribe to the school of thought (and it is just one school) that it is better to, as much as possible, let the function definitions serve as the declaration. This does a few things -- it eliminates the need to keep the declaration and the definition synced and it ensures that you do not inadvertently end up with circular function call chains.

#### Picbuster

Joined Dec 2, 2013
1,038
hey guys,
thank you for reading my thread, i am creating a program where a stepper motor does 1 step and reads sensor values, however, to read the sensor values i have to always do the following code:

char *temp = "0000";
int a=0;
ADCON0 = 0b10010000; //AN2 analog channel selected
ADCON1 = 0b01001001; //PORT configuratoin to AN2
TRISA = 0b00000100; //RA2 analogue input
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
do {
temp [0] = adc_value/1000 +48 ; //add 48 to get the ascii value
temp [1] = (adc_value/100) %10 +48 ;
temp [2] = (adc_value/10) %10 +48;
Lcd_Out(1,11,temp);
Delay_ms(100);

Lcd_Out(2,1,"Too hard ");

the above is quite long as i have to copy and paste the highlighted part again and again in the main code, i have tried doing it in a separate function, however, as i try to read the adc_value in the main function, it says that the function is undeclared (i know i have to copy the declaration but that would be the same thing all over), any suggestions would be appreciated, of if theres a better way...Thanks guys i just want to save the time of copying all that again and again and saving the hedache of checking incase there is a fault or change needed in the future
This allows you to have always the last value available if not wanted trigger each time with the 'get new value'command

{
PIR1bits.ADIF = 0; // Clear A/D conversion complete interrupt flag
Delay1KTCYx(5); // Delay for 5000 instruction cycles
Vin=Vin*0.0357;
ADCON0bits.GO = 1; get new value
}

//in main loop

printf( "value %ld",Vin);

#### vpoko

Joined Jan 5, 2012
267
No, you do not have to declare a function separately from its definition. I almost never declare function prototypes except in header files for functions that I want to use in files other than the ones in which they are defined. I subscribe to the school of thought (and it is just one school) that it is better to, as much as possible, let the function definitions serve as the declaration. This does a few things -- it eliminates the need to keep the declaration and the definition synced and it ensures that you do not inadvertently end up with circular function call chains.
In ANSI C99, it's required if you're calling a function that hasn't been defined by the time you call it (i.e., it's defined further down in the code, even in the same source file). To me, being allowed to "call up" but not "call down" is too weird of a restriction, so personally I'd prototype everything.

Last edited:

#### vpoko

Joined Jan 5, 2012
267
WBahn, could you clarify your circular call chains comment? Even after giving it some thought, it's going over my head.

#### WBahn

Joined Mar 31, 2012
26,434
WBahn, could you clarify your circular call chains comment? Even after giving it some thought, it's going over my head.
I'm simply talking about indirect recursion.

Consider the following:

C:
int bob(int n)
{
return sue(n);
}

int sue(int n)
{
return bob(n);
}
If I have prototypes above this code, it will compile without warnings or errors. But if I don't, then I will at least get warnings about the call to sue() within bob() using an assumed interface. The deliberate use of indirect recursion is rare enough that most instances when it occurs are the result of logic errors (typically when updating code), thus developing a coding style that catches indirect recursion and makes you do something out of the ordinary (use a function prototype, in this case) to allow it has value.