Programming Functions and why are they so special

800 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

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.

You are viewing 1 out of 12 answers, click here to view all answers.