I might have misunderstood what your question is, but anyway, hopefully the following is helpful.
What is a function?
A function is a "rule" that assigns to each element in a set $D$ (called the domain) exactly one element in a set $E$ (called the codomain).
The condomain is just the set where the values of the function are. All functions have domains and codomains. It is part of the definition of a function.
From the comments below, it seems like you might be asking why one can't just take the range as the codomain. The answer, I believe, is in the logic order. We can't ask about the range of a function without having a function. If we have a function, then we already have a codomain.
Now, if you have a function $f: D \to E$ with domain $D$ and codomain $E$, then you can define another function $f': D \to f(D)$ by $f'(x) = f(x)$. If $f$ us surjective, then $f = f'$ otherwise you have two different functions.
The range/image of a function $h: D \to E$ is the subset of the codomain $\{h(x) \in E : x\in D\}$. If the range and codomain are equal as sets, then we call the function surjective.
For example $f: \mathbb{R} \to \mathbb{R}$ given by $f(x) = x^2$ and $g:\mathbb{R} \to [0,\infty)$ given by $g(x) = x^2$ are two different functions because they have different codomains. For example, $f$ is not surjective, while $g$ is surjective.
If I meet you on the street and you tell me about this function called cosine. Then I can ask you what the domain and codomain is. You, technically, don't compute the codomain or the domain. They always follow the function. It makes no sense to talk about a function without the two accompanying sets.
Now in calculus, the codomain is usually assumed to be the real numbers. So here it makes sense to ask about what the range is. We have problems that ask for the domain, but what we usually mean is, as you say: find the set of real numbers where the expressions is defined.