error: expected unquaified-id before 'if'

Hello. I'm making a big project that senses if your garage is open, then calls you. This is my code:

#define trigPin 41
#define echoPin 40
#include <LiquidCrystal.h>

#include <SoftwareSerial.h>
#include <String.h>
#include <Keypad.h>

SoftwareSerial mySerial(7, 8);

boolean garageOpen = false;

long prevM = 0;
long interval = 10000;

const byte Rows = 4; //number of rows on the keypad i.e. 4
const byte Cols = 3; //number of columns on the keypad i.e. 3

char keymap[Rows][Cols] =
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};

byte rPins[Rows] = {A15, A14, A13, A12}; //Rows 0 to 3
byte cPins[Cols] = {A11, A10, A9}; //Columns 0 to 2

const int rs = 47, en = 46, d4 = 45, d5 = 44, d6 = 43, d7 = 42;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Keypad kpd = Keypad(makeKeymap(keymap), rPins, cPins, Rows, Cols);

char phoneNumber[] = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};

void setup() {
mySerial.begin(9600);
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.begin(16, 2);
lcd.print("phone number:");
}
void loop() {
char keypressed = kpd.getKey();
if (keypressed != NO_KEY)
{
Serial.println(keypressed);
}
lcd.print(keypressed);
}

if (garage()) {
unsigned long currentM = millis();
if (currentM - prevM > interval) {
Serial.println("garage open for 60 sec");
String number = "ATD + +13392236045;";
mySerial.println(number);
delay(7000);
mySerial.println("ATH");
prevM = currentM + 7000;
}
else if (!garage()) {
unsigned long currentM = millis();
prevM = currentM;
}
}
}
boolean garage() {
float duration, distance;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) * 0.0344;
if (distance >= 10) {
garageOpen = true;
return garageOpen;

}
else {
garageOpen = false;
return garageOpen;
}
}

I am very urgent because I have my robotics competition very soon this Saturday. Please give quick responses. Thanks!

-Kit-KatKarateKat ;D

Check your curly brackets. Where is the end of the loop() block?

if(garage())

And everything after is outside of loop().