In my mathematics class, i have learnt that a function from a set $A$ to a set $B$ is a subset of the cartesian product of $A$ and $B$, that is, $A$ X $B$ with the condition that the first sub-element of each element of the subset are distinct and that all the elements of A are present.
In some books i have read that it is a rule which which connects or maps the values of two variable quantities. Recently i saw that in a book it is written y is a function of x.
What exactly is a function and is it related to variables or sets?
To be very informal, functions are a way of representing any relation between two quantities, on the condition that they are uniquely related. Why is it that function are defined in such a way that the relation is unique? It is because otherwise functions would not be that useful as they are. Consider any expression in some variable $x$: $2x-5$, $x\sin(e^x)$, $\ln(\sin x)$, anything. And, try to calculate the values of each of these expressions for some particular $x$. Do the expressions produce multiple values, or just a single value?
Rigorous approach: Let there be two sets $A$ and $B$. Then, a function $f$ from $A$ to $B$, written as $f:A\to B$, is a subset of the cartesian product $A\times B$, such that all $(x,y)\in f$ have one and only one $y$ for each $x$. Here, $A$ is called the domain of $f$, and $B$ is called the co-domain of $f$. For all $y$ in $(x,y)\in f$, the notation $y=f(x)$ is used. The domain of $f$ can also be thought of all the arguments(inputs) $f$ can take to produce well defined results. And finally, the set of all $f(x),x\in A$ is called the Range of $f$, and intuitively, is the set of all outputs the function produces.