Andrew Kerr, who studies Mathematics at the University of Glasgow, grew cannabis at his home in Paisley in March this year.

The town’s sheriff court heard that officers discovered three plants during a search of Kerr’s home.

No valuation was given for the drugs, which 50-year-old Kerr admitted growing on March 18, 2015, at his home in the town’s Foxbar area.

Last month he pleaded guilty to breaking Section 4(2)(a) of the Misuse of Drugs Act 1971 by producing cannabis, which is a class B controlled drug.

Sentence was deferred for Kerr to be assessed by social workers and he returned to the dock this week to learn his fate with the threat of a prison term hanging over his head due to the serious offence he was convicted of and his previous drug convictions.

When he appeared before Sheriff Colin Pettigrew Manus Tolland, defending, said Kerr, who has just entered the second year of his maths degree at the University, “was willing and able to do unpaid work” and could also pay a fine.

As he spared Kerr, who works as a dog walker and earns around £100-per-week, the sheriff said: “A fine was never going to be imposed in relation to this.

“Your previous convictions, although drug related, are very old.

“There was a significant level of planning involved here - you know it’s illegal and you carry on regardless.

“The one factor keeping you out of jail today is the number of plants, which was relatively modest.

“I am in no way condoning the growing of cannabis.” He placed Kerr on a Community Payback Order, telling him to complete 200 hours’ unpaid work within nine months.

The order was imposed as a direct alternative to custody, meaning Kerr faces jail if he does not complete it in a satisfactory manner.