🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  • Ex. 1 - No return value
  • Ex. 2 - No parameters
  • Ex. 3 - Parameters
  • Ex. 4 - Optional Parameters
  • Ex. 5 - Return Values
  1. Unit 4: Functions

User Defined Functions

Writing your own functions gives the ability to reuse code.

PreviousFunctional ProgrammingNextHoisting and Scope

Last updated 5 years ago

Almost all programming languages allow us to write our own functions. In Javascript, the syntax is: function name (params) { // Your code with optional return value }

Let's say we need to do something over and over in our program - perhaps decide how many zeros are in a quadratic equation given a ,b ,and c. Rather than do the math over and over, we can create a function and return either 0, 1, or 2.

// Determine how many real roots exist
function descriminate(a, b, c) {
    let desc = Math.pow(b, 2) - (4*a*c);

    if (desc < 0)
        return 0;
    else if (desc === 0)
        return 1;
    else
        return 2;
}

This would allow us to make decisions or act on the return value of 0, 1, or 2. We could print it, use it for other math, or ignore it completely.

Ex. 1 - No return value

If your function does not have a return call with a value, the result is undefined. Here's a simple example of a function that doesn't return anything:

Ex. 2 - No parameters

It isn't often that a user-defined function has zero parameters. However, it can be done. Here's one that shouts the current time (it prints it to the console). No parameters, no return value.

Ex. 3 - Parameters

Here's a function that takes two parameters, base and exponent. It prints the answer to the console. Again, no return value.

Ex. 4 - Optional Parameters

Notice how we defined the parameter to our function: shoutCurrentTime(militaryTime = false) This defines the parameter and sets it to false, unless we set it to something else.

Ex. 5 - Return Values

Many examples in one:

In example 2, , the time is output in 24-hour "military" time. Perhaps we only want it to do that if we ask it to. We can have a parameter militaryTime that is false by default but if we pass true as a parameter to the function, it prints in 24-hour format.

The true strength of a function is the ability to return an answer. It could be a modification of the parameter(s) passed into the function or a completely unrelated piece of data. The return keyword breaks out of the function, no matter what is happening, and sends whatever data you want to return. Note: if you need to return more than one piece of data, you must return it in an or object.

above
array