Programming Functions and why are they so special

754 views

I’m still trying to wrap my ahead around this concept.

I’ve done basic maths, so I know that f(x) = x is like a function, but I can’t find an intuitive way of explaining why some functions don’t have to return values. In addition, what separates functions from just lines of code?

In: Technology

12 Answers

Anonymous 0 Comments

A void function (one that does not return a value) can be seen as a programmed routine. Most of the time, these functions are used to call other functions or to create/destroy global variables.

Anonymous 0 Comments

function in programming are not like math functions

a function in programming is an operation you perform on data, normally a simple task that can be split in its own block of code

it doesnt necessarly need ot return anything(tho in most cases it should even if its only to debug itself) since often all you need from the function was the operation it did on the variables that can be taken over by another part of the code, not their actual result.

Anonymous 0 Comments

a function is just shorthand for lines of code.

At the simplest level you’re basically you’re telling the computer “every time someone tries to execute a function, just replace it with all the lines of code in the definition of the function”

This allows code to be much more readable and smaller. If i have 50 lines of code that define, lets say a hotel scheduling software’s function “BookGuest(LastName, first name, room)”, and i use that 40 times, i can either have the 2000 lines of code from rewriting those 50 lines 40 different times, or i can have 90 lines of code, from the definition of 50 lines and then just one line for each of the 40 uses.

It also allows change super quickly, easily, and with less mistakes. Suppose i get a new Sales system, and oh no, i have to change 2 of the lines in the code of booking a guest. If i did it as a function, i just change 2 lines in the function, and them bam every time in the code it sees the functions name, it calls up the new and improved code.

If i instead typed out those 50 lines 40 times, i now have to go to all 40 uses and make sure i change those exact 2 lines the same in every spot, and make sure i hit all 40 and not 39 of the cases.

Anonymous 0 Comments

You use functions to break up the code into pieces. Each function should perform one task, if it’s more than that, it can be broken up into even more functions (that’s sort of what abstraction is).

Some functions may need to return a value, but some may not at all. If you have a function that just deletes a file, you don’t need to return anything from it. But if you have a function that adds two numbers, you need to return the result.

Anonymous 0 Comments

Aspring game developer here still studying undergraduate computer engineering course here:

I suppose depending on the type of language you are using, but this is purely if you were using object oriented programming. So, that we always have something getting passed around.

I feel like the easiest point to explain why there are functions don’t have return values is to tell people about getters, and setters. Which in simpler terms would be one gets values or ‘tell me what this is’, and setters which is ‘change this to that’.

So, like since computers are basically calculators, which sends electronic signals, which are sending whole lot of turned off (0) turned on(1) signals. So, imagine some guy that is blinking morse code (flat, short) with light switch at insanely fast speeds, while another guy is able to decode it just as fast. So, someone hundreds of kilometers away outside auditory range can write the entire script of Shakespeare this way.

A function that has get values will tell you what a value is. It has a return value. You go ‘hey bruh tell me how much this pizza is’ and the function will silently nod and give you a return value ‘2.00 without tax’. If this was code it’d be like:

int howMuch () { return price; }

A set value doesn’t need a return value, it however needs a value for you to put in. You go ‘look mate, I only got a dollar, can you give me pizza for a dollar anyway, I am really hungry’ It might look at you, and if the function lets you change values, it will silently nod and give you the pizza without saying a word. If you said nothing, to a setter, the setter might not know what to do, and you won’t ever get a pizza for a different price, no matter how long you stood there, and the exception handler will throw you out because you are threatening the whole pizza store by using the pizza store incorrectly.

void sellMePizzaFor(int newPrice) { pizza.price = newPrice; }

Of course, you can have more than one input in to this method, like:

void sellMePizzaAndDrinksFor(int PizzaPrice, int DrinkPrice, int SecondPizzaPrice, int Second DrinkPrice) { pizzaOne.price = PizzaPrice, DrinkOne.Price = DrinkPrice; PizzaTwo.price = SecondPizzaPrice; DrinkTwo.price = SecondDrinkPrice;}

All of the functions can only return only one or zero values at a time. For this let’s imagine anything that can be a value is something you can hold in your hand. From nothing, millions of functions, from some ancient dna evolving in to a pig, wheat, cow etc to eventually give their own DNA, or value to something else for it too can pass on their values to the time we are talking about to reach some oven for it to be the pizza/object value it is today But, that’s besides the point.

As for lines of code, it could just be a part of a function. It can’t do anything on it’s own. Imagine a five year old that asks about everything.

Like a line of code might say

“color of pig = color of mother pig + color of father pig”

If it’s a function it would have had the sense to ask which one is the mother pig, and which one is the father pig. By code of line without being a function that is able to recieve the exact identity of the parent pigs (or values). It will be like what mother pig, what father pig, and it won’t know what to put down the color of pig it is producing. Like some stranger came up to you with no context, and asked “what is x when f(x) = x” and fully expected an integer as an answer instead of hearing about what that would look like in a graph or something. If they specified what the function was like what is x when f(x) = x if x was 1, it would make sense for you to tell them what the x was in an integer. Since now you can give give out a value (which don’t have to be always integer, that was just an example.) . Also, if you didn’t specify what a color was, you would have to start telling the program what a color was in the first place. So, that’s what you use the ‘library’ for. So, you don’t need to go around telling every children/program what a color is, and just tell them ‘here is a library, get your own education’. Except you are just relying on people smarter than you to tell them what a color is, so if you use color incorrectly yourself like color = color of my loneliness, you are still getting nowhere because the child doesn’t know what you are on about since it is smart enough to know such thing isn’t an actual color, unless you told them what you were on about in advance.

Anonymous 0 Comments

An example of a `void` function might be one that just prints some text, like a really basic info/welcome message that never changes — you don’t need a return value from that.

Anonymous 0 Comments

A function like `f(x) = x + 1`, is a pure function. It takes a value and adds one to it. It is considered `pure` because the output value depends only on the inputs to the function. For example, `f(2)` always returns `3`. And it does not interact with the outside world in anyway. In other words, it is free of side-effects.

A function like: `hi(name) = print (“Hello, ” ++ name ++ “. How are you?”)` doesn’t return any useful value. It is only useful because it causes side-effects. The side effect in this case is causing characters to be printed to the screen. Because it has side-effects it is an impure function.

A function allows you to give a name to a bunch of lines of code. Instead of having to copy and paste those lines of code every time you want to use them, you can just call the name of the function instead.

A function also provides a way to make parts of the function variable. For example, in the `hi` function above, the `name` is variable. So instead of having to write:

print (“Hello John. How are you?”)
print (“hello Mary. How are you?”)

You can write:

hi “John”
hi “Mary”

Notice above how I forgot to capitalize the `h` in `Hello Mary.`. By using a function, I only have to get it right in one place. And if I wanted to change it to say, `”How are you today?”`, I only need to update things in one place.

Modern programming languages often use the term `function` differently from the way mathematicians use the term. Though some languages are closer to the mathematical usage than others. For example, in Pascal, a routine is called a `function` if it returns a value and a `procedure` if it doesn’t.

In some languages, like ML, a function always takes a value and returns a value — much like a standard mathematical function. If you don’t have anything useful to pass to the function or return, you use the value `()`. For example, consider this function:

let hello () = print “hello, world”

To call that function you would write:

hello ()

the `print` function doesn’t have anything useful to return, so it just returns `()`.

So `hello` takes the value `()` and returns the value `()`. But it has the useful side-effect of printing something to the screen.

Other languages, like Haskell, allow you to mark if a function is pure or not. For example, this function:

f :: Int -> Int
f x = x + 1

has the type `Int -> Int`. So we know it is taking an integer and returning an integer, and not interacting with the outside world at all. We do not have to see the function implementation to know that. If there is a function with the type `g :: Int -> Int`, we know it is also pure with out knowing what it actually does.

If we consider this function:

hi :: String -> IO ()
hi name = putStrLn (“Hello, ” <> name <> “. How are you?”)

We see that it takes a `String` and performs some `IO` (input/output — in other words, causes side effects and interacts with the outside world). That IO does not produce any useful return value, so it just returns `()`.

The idea of expressing computation using function abstraction and application and variables actually predates digital computers. Alonzo Church invented Lambda Calculus in the 1930s. The second oldest programming language still in use today, LISP, was based heavily on lambda calculus. And newer languages like Haskell are essentially just very fancy versions of Lambda Calculus.

In the Haskell example, we gave the functions `type signatures` which described their input and output values. Type Theory was actually invented even earlier than lambda calculus. Bertrand Russell begin experimenting with ‘theories of types’ in 1902 and eventually solidified those theories in Whitehead and Russell’s Principia Mathematica published between 1910 and 1913.

Modern programming languages have gone in two directions. Some have focused on the “git ‘er done” and have thrown out the math and formal reasoning aspects of lambda calculus and type theory.

Other languages, like Agda, Idris, and Coq have gone the other direction and worked to create programming languages that not only allow you to have useful side effects, but also allow formal mathematically reasoning and proofs about the behavior of the code.

Anonymous 0 Comments

> but I can’t find an intuitive way of explaining why some functions don’t have to return values

There are lots of other things they could be doing where you don’t necessarily need an output, such as displaying something for the user. I guess in that situation it could still be returning a bool or int to let you know it succeeded or failed, but that’s not always necessary.

> what separates functions from just lines of code?

Technically nothing, but in practice the difference is re-usability. If you’re writing the same lines of code over and over and over you should be putting them in a function. Not only does it keep for file size smaller (which is a big deal in some environments like web development), but then if you have to change that code later you only have to change one place instead of all of them.

Anonymous 0 Comments

( sorry on mobile) Forget the mathematics concept for a bit and use this analogy. Imagine the function is a servant that does something. For instance, you can have a servant that you give an empty glass of water and his “function” is to go fill it up and “return” it to you. Now you can have another servant that is tasked with disposing your trash, you give him the trash and he disposes of it and doesn’t need to return anything to you. But sometimes you may want the servant to say send an important letter but let’s say the post office was closed. You don’t want him to just do nothing about it, so he returns with a message telling you it’s closed and he couldn’t do the task. So similarly, a function can return you a value so you can do something about it, or they just perform a task that you don’t need anything to be returned at the end of, or to return a value or something to deal with it if it fails. Alternatively you can throw an exception but you get the point.

Anonymous 0 Comments

The ones that don’t return anything are for being read by a person. Not read into. They are supposed to allow you to think instead of doing the math and then you can continue if you are doing it. A computer will only send to the screen in those events, and the rest of it is doing nothing by then. In otherwise, the computer may hold a chip that will expect to be receiving input and that will endanger the process become a “single” operation and not just a “dedicated” one.