Skip to content

Latest commit

 

History

History
133 lines (92 loc) · 3.38 KB

function.md

File metadata and controls

133 lines (92 loc) · 3.38 KB

Javascript Functions

We can make functions in Javascript using "function declarations", like this:

function addTwoNumbers(first, second) {
	return first + second;
}

(note that there's no semicolon at the end of a function declaration)

We can also create functions using a "function expression", where we create a function and bind it to a variable. When we do it this way, we typically don't give the function a name (after the word function and before the parameter list); this is called an "anonymous function".

Here, we make an anonymous function using a function expression, and assign it to a new variable:

var alsoAddsTwoNumbers = function (first, second) {
    return first + second;
};

(note that there is a semicolon at the end of an assignment like this—after all, this is not all that different than var i = 7;, except with a function as the things-we're-assigning-to-the-variable.)

Both of these create functions that do the same thing, so it's often a matter of style and taste which style you will you.

Either you, you call them like this:

addTwoNumbers(5, 10);        // --> 15
alsoAddsTwoNumbers(5, 10);   // --> 15

In Javascript, we can pass functions to other functions as arguments.

function batman(exclamation) {
	console.log("NaNaNaNaNaNaNaNa");
	exclamation();
}

// And then, we can pass a function we have previously declared
function obvious() {
	console.log("Batman!");
}

batman(obvious);

The obvious function is called exclamation inside of the batman function, and when it gets called, it runs the code inside of obvious—printing "Batman!".

You can do this with functions you separately defined, like that, but you can also use function expressions directly:

function batman(exclamation) {
	console.log("NaNaNaNaNaNaNaNa");
	exclamation();
}

batman(function () {
	console.log("CAT MAN!");
});

This passes the newly-defined, anonymous function to the batman function. The result is the same, though—it calls that new function exclamation and then calls it, printing, in this case, "CAT MAN!".

While creating function expressions and passing them into as parameters into other functions may seem confusing the first few times you see it, it's worthwhile to study this carefully—this is a common practice in Javascript, as scripts often use this technique.

Python Functions

In Python, we have one traditional way to create functions:

def add_two_numbers(first, second):
	return first + second

We can call that function either using named parameters or positional parameters:

add_two_numbers(5, 10)                 # --> 15

add_two_numbers(first=5, second=10)    # --> 15

In Python, there's an analogous feature for creating function expressions, called "lambdas". Lambdas are functions that you create like an expression, but the syntax is a bit different:

also_adds_two_numbers = lambda first, second: first + second

(Lambdas are less powerful than full functions, as they can have only a single expression as the body, and it's always returned. Therefore, they are not used for commonly).

You can call a Python function and pass it another function, similar to Javascript:

def batman(exclamation):
	print "NaNaNaNaNaNaNaNa" 
	exclamation()

def obvious():
	print "Batman!"

batman(obvious)