Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between functions in math and functions in programming?

Tags:

c

function

math

What is the difference between functions in math and functions in programming?

like image 735
Alon Gubkin Avatar asked Aug 31 '10 01:08

Alon Gubkin


People also ask

What are functions in programming?

A function is simply a “chunk” of code that you can use over and over again, rather than writing it out multiple times. Functions enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task.

What are mathematical functions in computer?

Used in programming, functions take an input, perform some operation on it and returns the output. A mathematical function works the same way. Given an input a function must always return the same answer. For example, given f(A) = B then the function a must always return the value B.

What are the 4 types of functions?

The types of functions can be broadly classified into four types. Based on Element: One to one Function, many to one function, onto function, one to one and onto function, into function.

What are the 3 types of functions?

There are 3 types of functions: Linear. Quadratic. Exponential.


2 Answers

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 ALWAYS if you call f(x) in math, you will get the same answer, but if you call f'(x) in C, the answer may not be the same (to same arguments don't get the same output).( I think this has a bit of false.. If you have two exactly machine in the same status, they will output the same.. but what it tries to say is that a function in non-functional languages may not depend solely on the arguments you give them, but on other things of the program)

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).

like image 149
Marco Avatar answered Oct 16 '22 05:10

Marco


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?:

int 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.

like image 28
San Jacinto Avatar answered Oct 16 '22 06:10

San Jacinto