Skip to content

Introduction to JavaScript Functions #3

@Coderepublica

Description

@Coderepublica

Functions

  1. Write function declarations and function expressions
  2. Differentiate console.log and return
  3. Define functions that take multiple arguments
  • Understand the use of functions (i.e. what's the function of a function?)
  • Define a function without arguments
  • Define a function with arguments
  • Return a value from a function

What Are Functions?

Function are just reusable bits of code. For example, see below (without current value).

function doSomething() {
	console.log("Hello World");
	console.log("How are you today?");
}

And then when you recall it:

doSomething() 

// 
Hello World
How are you today?

Here's an idea to fix a function within another:

function chorus(){
	console.log("Whoa, we're half way there");
	console.log("Whoa, livin' on a prayer");
	console.log("Take my hand and we'll make it - I swear");
	console.log("Whoa, livin' on a prayer");
}

function chorusx4(){
	//print 4x4 lines of lyrics
	chorus()
	chorus()
	chorus()
	chorus()
}

What Are Arguments?

Arguments are basically whatever input you place inside () of function.

Here's an example:

function DeclareAge(age) {
	console.log("I am " + age + " years old.")
}

DeclareAge(18) // I am 18 years old.

What's the return Keyword?

The first thing you need to know is that every function returns something. But if you don't specify what to return or ask the console for a function, then it's going to return "undefined."

You use return when you want to "capture" or remember the result of a function.

Once it returns, the functions has completed execution - in other words, the function is OVER.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions