In functional programming you have Referential Transparency, which means that you can replace a function with its value without altering the program. This is true in Math too, but this is not always true in Imperative languages.
A math function is defined by: a relationship that maps elements from one set (A) to another (B), mapping each element of the first set with only one of the other set. In C (as in other programming languages) this is also true, you have your input set, and your output set (which is almost always only ONE).
The main difference, is, then, that if you call f(x) in math, you will ALWAYS get the same answer, but if you call f'(x) in C, the answer may not be the same - the same arguments don't always return the same output. A function in non-functional languages may not depend solely on the arguments you give them, but on other things in the program (enclosing scope, globals, builtins, state if you're using object orientation).
Another difference between math and C functions, is that in Math you can't make a function that goes from a non-empty set to an empty set (in C this would be: You aren't requiered to always return something with your function). Also, not all function are computable (I don't know if there's something similiar to this in math..). You don't have functions for infinite sets (you have finite memory, so the set of the possible input parameters must be finite), but in math, you can define a function for infinite sets (like f: N -> N) and for uncountable sets (like f: R -> R) (In C you have floating point numbers, but they only represent a reduced set of real numbers, which is finite).
Summarizing:
In C you don't always have Referential Transparency. Your functions may not always give the same output for the same input parameters. You can have Math functions that defined for an infinite set of inputs, but in C functions your input is finite. In C functions you can have functions that returns nothing, but in Math you can't have that (if you have a function that has a non empty input set, you must map each element with one of another set).
Answer from Marco on Stack OverflowIn functional programming you have Referential Transparency, which means that you can replace a function with its value without altering the program. This is true in Math too, but this is not always true in Imperative languages.
A math function is defined by: a relationship that maps elements from one set (A) to another (B), mapping each element of the first set with only one of the other set. In C (as in other programming languages) this is also true, you have your input set, and your output set (which is almost always only ONE).
The main difference, is, then, that if you call f(x) in math, you will ALWAYS get the same answer, but if you call f'(x) in C, the answer may not be the same - the same arguments don't always return the same output. A function in non-functional languages may not depend solely on the arguments you give them, but on other things in the program (enclosing scope, globals, builtins, state if you're using object orientation).
Another difference between math and C functions, is that in Math you can't make a function that goes from a non-empty set to an empty set (in C this would be: You aren't requiered to always return something with your function). Also, not all function are computable (I don't know if there's something similiar to this in math..). You don't have functions for infinite sets (you have finite memory, so the set of the possible input parameters must be finite), but in math, you can define a function for infinite sets (like f: N -> N) and for uncountable sets (like f: R -> R) (In C you have floating point numbers, but they only represent a reduced set of real numbers, which is finite).
Summarizing:
In C you don't always have Referential Transparency. Your functions may not always give the same output for the same input parameters. You can have Math functions that defined for an infinite set of inputs, but in C functions your input is finite. In C functions you can have functions that returns nothing, but in Math you can't have that (if you have a function that has a non empty input set, you must map each element with one of another set).
It depends on the domain (I don't mean the domain of the function, I mean the domain of study) and also possibly the language.
In math, a function has an input that maps to only one output for a given input value (vertical line test, remember). In programming, this might not be strictly the same, depending on where you draw the line between "input" and "function logic."
For instance, let's imagine we have a function rand() that reads atmospheric conditions to arrive at a truly random number. Let's also imagine that a calling function takes one integer parameter as a mutiplier of sorts. Is the following a function?:
Copyint giveRandAtmosWithMul(int mult)
{
return mult * rand();
}
In the mathematic sense, it probably is not a function if you consider mult as the only input to the problem, but clearly rand() is offering input as well (even though rand() always has the same entry point in machine code).
As you can see, the differences aren't really objectively definable without some standard protocol that everybody agrees to.
I'm new so sorry if this appears silly but are functions in programming inspired by functions in maths and are operators nothing but functions.
Why are functions in programming not considered "mathematical" functions?
Is recursion in programming the same thing as recursion in math?
How much math is prevalent in programming? What types of math are used frequently aside from basic arithmetic?
Can I use math functions in C without math.h?
Can I create my own math functions in C?
What is the return type of most math functions?
Videos
I think functions in maths and programming sound too similar and can't operators be modelled by functions themselves? I think operators can be defined via functions. I think learning functions in maths would be more helpful for learning programming. Am I right in these conclusions or is it wrong.
In functional programming you have Referential Transparency, which means that you can replace a function with its value without altering the program. This is true in Math too, but this is not always true in Imperative languages.
A math function is defined by: a relationship that maps elements from one set (A) to another (B), mapping each element of the first set with only one of the other set. In C (as in other programming languages) this is also true, you have your input set, and your output set (which is almost always only ONE).
The main difference, is, then, that if you call f(x) in math, you will ALWAYS get the same answer, but if you call f'(x) in C, the answer may not be the same - the same arguments don't always return the same output. A function in non-functional languages may not depend solely on the arguments you give them, but on other things in the program (enclosing scope, globals, builtins, state if you're using object orientation).
Another difference between math and C functions, is that in Math you can't make a function that goes from a non-empty set to an empty set (in C this would be: You aren't requiered to always return something with your function). Also, not all function are computable (I don't know if there's something similiar to this in math..). You don't have functions for infinite sets (you have finite memory, so the set of the possible input parameters must be finite), but in math, you can define a function for infinite sets (like f: N -> N) and for uncountable sets (like f: R -> R) (In C you have floating point numbers, but they only represent a reduced set of real numbers, which is finite).
Summarizing:
In C you don't always have Referential Transparency. Your functions may not always give the same output for the same input parameters. You can have Math functions that defined for an infinite set of inputs, but in C functions your input is finite. In C functions you can have functions that returns nothing, but in Math you can't have that (if you have a function that has a non empty input set, you must map each element with one of another set).
Answer from Marco on Stack Overflow