FAQ: Functions - What are functions?

This community-built FAQ covers the “What are functions?” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise What are functions?

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

fine. this is silly excercise.

1 Like

i concur… will i be able to go through after i post this?

Wait @danraphv, what’s wrong with this lesson?

i can´t go through in code academy, i’m stuck on the first lesson and the next button won’t activate

What browser are you using, and have you tried reloading your page, restarting your browser, clearing your cookie cache, and restarting your computer?

I can’t click on the next button. Using Google Chrome

I’m having the same problem. I’m using Google Chrome…

Have you tried all the recommend solutions?

i am also using google chrome, and not able to move to the next slide in the lesson. should i do something? its not mentioned in the instructions

This link will take you to the second lesson/exercise…

https://www.codecademy.com/courses/introduction-to-javascript/lessons/functions/exercises/function-declaration

3 Likes

why can’t i just move to next execise after watching the gif?

Unfortunately this is an issue with the lesson.
The bug has been reported and engineers have been informed.

The work around is: Refresh the page / Come out of the lesson, go back / Get Help > solution

So how do you move on in the lesson??

The problem is still here and still I cannot move on to next step.

The example given in this exercise is incorrect right? It was meant to be width * length for the area of a rectangle? Or am I rerard?

How similar is function in math to function in programming? Are they the same, or different?

In math a function must return at most one and only one value.

Are functions and methods the same?

In JS a method is a specific type of function that is a property of an object, and can only be accessed by those types of objects. AFAIK there are two types of methods: static methods, and instance methods. A static method can be used on the class of objects itself, a great example is the Math.random() method, as you are calling it on the Math global object. On the other hand, an instance method must be called on an instance of an object. An example is String.prototype.toLowerCase(). When you use that method, you would have an instance of a string such as var str = “my string” and you would call it as str.toLowerCase() (note that you do not include “prototype” when calling the method).

Math and programming functions are very similar. They both follow the form: input => set of instructions => output. A function in JS can only have one return value, however, it could be any type of data, such as an array, which would allow you to return many values.

1 Like

A math function is a special relationship between two values, one a dependent variable and the other independent. When we say, ‘y is a function of x’ we mean that y varies as x such that no two y values can have the same value for x.

y = mx + b

y = f(x)

f(x) = mx + b

The above function graphs a straight line that passes through all points, (x, y) when plotted. It rises from left to right when m is positive, and falls from left to right when m is negative.

y = ax^2 + bx + c

f(x) = y

f(x) = ax^2 + bx + c

The above function graphs a parabola which locus passes through all the pionts, (x, y). When a is positive, the graph opens upward and reflects about the minimum, which is at the midpoint of the curve. When a is negative, the graph opens downward and reflects about the maximum, which is also the midpoint of the curve. In both cases, maximum and minimum the instantaneous slope of the curve is zero.

This function becomes a relation (not a function, as such) if we rotate it such that the curve opens up to the right or the left. A vertical line can be passed through two points on the curve, which does not satisfy the definition of a function.

We apply the term, ‘function’ in programming to a block of code that takes in an argument and returns an outcome. When the outcomes are always predictable for the same inputs and have no interaction with the outside and no side effects we call them, ‘pure functions’. The definition of a general function in programming does not imply different outcomes with different inputs, and does not infer any relationship between the inputs and the outputs. This is, so to speak, a gray area, so it is best to not see them in the same light as a function in maths. Instead see them as reusable blocks of code that can give us predictable results. Think ‘action’ rather than ‘relation’.

In nearly every sense, yes. A method is a function but rather than taking inputs from the outside, the object provides the arguments (the execution context). That is why we always see ‘this.property’, where ‘property’ is an attribute of the object referenced by ‘this’.

const foo = function (bar) {
    return bar
}

as opposed to,

class Foo {
    constructor (bar) {
        this.bar = bar
    }
    foo () {
        return this.bar
    }
}

or more simply in an object literal,

object = {
    bar = "baz",
    foo () {
        return this.bar
    }
}

Notice that functions and methods exhibit the same behavior, but they act upon different arguments.

3 Likes