Functions at the end or beggining?

Hi,

I have noticed that when i flashother peoples code and use it there functions are after the loop, but when i try this it says that they arent recognised within my loop when they are called when compiled.

If I then shift the functions before the setup there are no errors when being compiled. What am i missing here? I would be nicer to have them at the end just so it is neater and also stop me scrolling up and down as much.

Thanks

What am i missing here?

Some code that illustrates the problem.

One of the things that the IDE does is to generate function prototypes. Without them, the function must be defined before it is used. With them, the order of functions in the source file doesn't matter.

C/C++ requires that function prototypes (Google it) are declared before the function can be used in the program and these are normally placed near the start of the programs for obvious reasons. The actual function code can be placed anywhere in the program as long as it is not within another function.

The Arduino environment creates the function prototypes for you behind the scenes so you don't have to and merges them in at the start of the program before compiling it. However, under some circumstances it does not always work.

Can you please post an example of a program that fails to compile when the functions are at the end of the code and where you have not manually added an explicit function prototype.

So how would you do that?

#define someFunction

void setup(){
 someFunction();

}

void loop(){

  }

void someFunction() {

some function in here

}

I'll post my actual working code in about an hour when i get on my laptop where ive had to place the function before the setup.

In my code I don't think I #defined any of the functions. is this critical.

So how would you do that?

Do what?

Clearly, you did NOT google "function prototype".

void someFunction(); // This is a prototype for a function that takes no arguments and returns nothing.

Sorry Paul, I have googled it but i'm struggling to understand it.

So when creating the function i would place it as the previous code i posted. This is how other code i download is written but i do the same and it wont compile unless I place the following before i actually call the function either in the setup of before the setup.

void someFunction();

Apologies if this is frustrating im still a bit of a newbie and am trying to understand it not just do it.

Apologies if this is frustrating im still a bit of a newbie and am trying to understand it not just do it.

Post the code that is causing problem. We can explain why, and how to fix it.

No probs. It'll be with you in about 20 mins when i get on my lunch break.

Please see my code below. I must say its for a STM32 blue pill but i wouldn’t think that should matter would it? :neutral_face:

I’v had to delete some of the functions as it exceeded the 9000 limit

#include "XPT2046_touch.h"
#include "SPI.h"

#include <Adafruit_GFX_AS.h>    // Core graphics library, with extra fonts.
#include <Adafruit_ILI9341_STM.h> // STM32 DMA Hardware-specific library
#include <FreeMonoBold9pt7b.h>
#include <FreeMonoBold12pt7b.h>
#include <FreeMonoBold18pt7b.h>
#include <FreeMonoBold24pt7b.h>

// For the Adafruit shield, these are the default.
#define TFT_CS         PA0                  
#define TFT_DC         PA1               
#define TFT_RST        PA2
#define CS_PIN PA4 // Chip Select pin

char currentPage;
uint16_t xy[2];
int px = xy[0];
int py = xy[1];
byte SB = 0;
byte BO = 0;
 
SPIClass mySPI(2); //Create an SPI instance on SPI1 port.
XPT2046_touch ts(CS_PIN, mySPI); // Chip Select pin, SPI port
Adafruit_ILI9341_STM tft = Adafruit_ILI9341_STM(TFT_CS, TFT_DC, TFT_RST); // Use hardware SPI





void setup() {
  Serial.begin(115200);
  tft.begin();
  ts.begin(); //Begin TouchScreen.
  
  tft.fillScreen(ILI9341_BLACK);
  tft.setRotation(1);
  currentPage = '0';
   
  //drawSpringBack();
  drawhomeScreen();
   //settingsScreen();
}


void loop(void) {

    if (currentPage == '0') {
    if(ts.read_XY(xy)){
      getXY();
     //Serial.print("x=");
       // Serial.println(px);
        //Serial.print("y=");
        //Serial.println(py);
      if ((px>=20) && (px<=300) && (py>=100) && (py<=150)) {
        
        currentPage = '1'; 
        tft.fillScreen(ILI9341_BLACK);
        settingsScreen(); 
      }
     
      /*if ((x>=35) && (x<=285) && (y>=140) && (y<=180)) {
        drawFrame(35, 140, 285, 180);
        currentPage = '2';
        myGLCD.clrScr();
        drawLedControl();*/
        }
    }

    if (currentPage == '1') {
    if(ts.read_XY(xy)){
      getXY();
     
      if ((px>=20) && (px<=150) && (py>=190) && (py<=210)) {
        currentPage = '0'; 
        tft.fillScreen(ILI9341_BLACK);
        drawhomeScreen();
      }
      if ((px>=20) && (px<=300) && (py>=10) && (py<=80)) {
        currentPage = '2'; 
        tft.fillScreen(ILI9341_BLACK);
        drawSpringBack();
          }
      if ((px>=20) && (px<=300) && (py>=100) && (py<=150)) {
        currentPage = '3'; 
        tft.fillScreen(ILI9341_BLACK);
        drawBackOff();
          }
    }
}

    if (currentPage == '2') {
    if(ts.read_XY(xy)){
      getXY();
     
      if ((px>=240) && (px<=300) && (py>=100) && (py<=150)) {
        
        Serial.print("x=");
        Serial.println(px);
        Serial.print("y=");
        Serial.println(py);
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_BLACK);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(SB);
        SB++;
        if (SB > 15){
          SB = 0;
          }
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_WHITE);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(SB);
        delay(300);
        }

        if ((px>=240) && (px<=300) && (py>=170) && (py<=220)) {
        
        Serial.print("x=");
        Serial.println(px);
        Serial.print("y=");
        Serial.println(py);
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_BLACK);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(SB);
        SB--;

        if (SB > 15){
          SB = 15;
          }
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_WHITE);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(SB);
        delay(300);
        }
        
        if ((px>=20) && (px<=80) && (py>=100) && (py<=220)) {
          currentPage = '1'; 
        tft.fillScreen(ILI9341_BLACK);
        settingsScreen(); 
          }
    
    }
}

        if (currentPage == '3') {
            if(ts.read_XY(xy)){
            getXY();
     
          if ((px>=240) && (px<=300) && (py>=100) && (py<=150)) {
        
        //Serial.print("x=");
        //Serial.println(px);
        //Serial.print("y=");
        //Serial.println(py);
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_BLACK);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(BO);
        BO++;
        if (BO > 25){
          BO = 0;
          }
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_WHITE);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(BO);
        delay(300);
        }

        if ((px>=240) && (px<=300) && (py>=170) && (py<=220)) {
        
       // Serial.print("x=");
        //Serial.println(px);
        //Serial.print("y=");
        //Serial.println(py);
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_BLACK);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(BO);
        BO--;

        if (BO > 25){
          BO = 25;
          }
        tft.setCursor(100, 195);
        tft.setTextColor(ILI9341_WHITE);
        tft.setFont(&FreeMonoBold24pt7b);
        tft.setTextSize(2);
        tft.println(BO);
        delay(300);
        }
       if ((px>=20) && (px<=80) && (py>=100) && (py<=220)) {
          currentPage = '1'; 
        tft.fillScreen(ILI9341_BLACK);
        settingsScreen();
    }
}
}
}

void settingsScreen(){       //Screen "1"
tft.fillRoundRect(20, 10, 280, 70, 20, ILI9341_RED);
tft.setCursor(40, 50);
tft.setTextColor(ILI9341_WHITE);
tft.setFont(&FreeMonoBold18pt7b);
tft.setTextSize(1);
tft.println("SPRING BACK");

tft.fillRoundRect(20, 100, 280, 70, 20, ILI9341_RED);
tft.setCursor(47, 147);
tft.setTextColor(ILI9341_WHITE);
tft.setFont(&FreeMonoBold24pt7b);
tft.setTextSize(1);
tft.println("BACK-OFF");

tft.fillRoundRect(20, 190, 130, 50, 20, ILI9341_RED);
tft.setCursor(65, 225);
tft.setTextColor(ILI9341_WHITE);
tft.setFont(&FreeMonoBold24pt7b);
tft.setTextSize(1);
tft.println("<");
}

void drawSpringBack(){
  tft.setCursor(10, 10);
  tft.setTextColor(ILI9341_WHITE);
  tft.setFont(&FreeMonoBold9pt7b);
  tft.println("This setting bends pipe over");
  tft.setCursor(10, 25);
  tft.println("the desired bend angle to");
  tft.setCursor(10, 40);
  tft.println("compensate for spring back");
  tft.setCursor(10, 55);
  tft.println("of the pipe, select an angle");
  tft.setCursor(10, 70);
  tft.println("after trial run");

  tft.fillRoundRect(240, 100, 60, 50, 10, ILI9341_RED);
  tft.setCursor(255, 135);
  tft.setTextColor(ILI9341_WHITE);
  tft.setFont(&FreeMonoBold24pt7b);
  tft.println("+");
  
  tft.fillRoundRect(240, 170, 60, 50, 10, ILI9341_RED);
  tft.setCursor(255, 205);
  tft.setTextColor(ILI9341_WHITE);
  tft.setFont(&FreeMonoBold24pt7b);
  tft.println("-");

  tft.fillRoundRect(20, 100, 60, 120, 10, ILI9341_RED);
  tft.setCursor(35, 170);
  tft.setTextColor(ILI9341_WHITE);
  tft.setFont(&FreeMonoBold24pt7b);
  tft.println("<");

  tft.setCursor(100, 195);
  tft.setTextColor(ILI9341_WHITE);
  tft.setFont(&FreeMonoBold24pt7b);
  tft.setTextSize(2);
  tft.println(SB);
  
  }

Please post the full error message that you get when you compile the code.

Well that is weird.

I have just compiled the code no problem after I cut the functions back to the bottom of the sketch to copy the code to here.

Last night it wouldn't work unless I had them above the setup.

I'll rewrite some code and try and replicate the issue I had and post it tonight when i finish work.

dave_morl: Well that is weird.

I have just compiled the code no problem after I cut the functions back to the bottom of the sketch to copy the code to here.

Last night it wouldn't work unless I had them above the setup.

I'll rewrite some code and try and replicate the issue I had and post it tonight when i finish work.

Looking at the way your code in reply #8 is indented I suspect that it's not weird. You more than likely had a function defined inside another one.