Functions in programming

Are functions in mathematics similar to functions in programming. Am I right yes or no?


Exactly the same.
In mathematics I give a function an input and the function does it’s “thing” and hands me back an output.

Functions are similar in programming

Think of a function that squares a number

int square(int n) {
    return n*n

You give the function it’s input “n”
from there the function mutates n by multiplying it by itself
it returns this new output to you.

1 Like

Not so. Similar in an anologous way, but not exact.

Functions in programming are re-usable code blocks that can take any form. In maths they refer to variation of y in terms of x.

y = f(x)

y varies as x according to the relationship defined in the equation.

f(x) = ax + b

where a and b are fixed, and x is the independent variable. When f(x) returns more than one value for y for one input of x, it is no longer a function, but a relation.

Take for instance a standard quadratic,

f(x) = ax^2

which produces a parabola centered about the original and reflected off the y axis. Each value of x yields only one value for y. If we rotate the graph 90 degrees clockwise or counterclockwise it is no longer a function since we get two values for y for one value of x.

x = ay^2

which when we solve for y, becomes,

y = +/-(x / a) ^ 0.5

As we can see, there are now two solutions.

We can write program functions that emulate maths, but that is where the similarity ends.


Is the definition of return to give something back as an answer? Am I right yes or no?

More or less, yes. A return is the response of the function to the caller. The caller is in a scope different from the function, and return bridges the scope gap.

I want to create a site and I find it difficult to create a site without taking a model I can’t apply what I learnt before can you help me?