🖥️
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
  • Parameters
  • Return Values
  1. Unit 4: Functions

Functional Programming

PreviousDebuggingNextUser Defined Functions

Last updated 6 years ago

Code runs in a very linear fashion: One line at a time, moving to the next.

The only method we have to repeat code is with a loop. This can create very long code files with a lot of repetition. What if you need to create a new mathematical operation and use it many times throughout the course of your program?

Functional Programming is a way of creating blocks (chunks) of code that you can use whenever you need. You can "call" a function that does some work and may or may not return a value.

We have been making calls to functions this entire time with console.log() and Math.floor() (for examples). When we ask the console to log() we have to put text inside the round brackets. The console prints out that text for us. When we call the floor() function, the removes any decimals on the number we place inside the brackets of floor() and returns the new value.

Parameters

The value(s) placed inside the brackets of a function are called parameters. They are also called inputs. Examples:

  • console.clear() takes zero parameters - it just does the job

  • Math.floor() takes one parameter - the value to floor

    • Math.floor(3.14159) returns 3

  • Math.pow() takes two parameters - the base and the exponent, separated by commas

    • Math.pow(2, 3) returns 8

  • console.log() takes any number of parameters, separated by commas.

    • console.log("The value %d is the floored value %f.", 3, 3.14159)

Return Values

Some functions (not all) give back data - a number, some text, an array, etc... This is called a return value. This value can either be used or ignored. Consider this - when you insert money into a vending machine, it returns either your money or a treat. When you throw money into a wish fountain, you do not receive anything tangible in return.

When a function returns a value, it is up to the programmer to decide how to use it.

Math.floor(6.87);               // The output 6 will go NOWHERE

let ans = Math.floor(6.87);     // The output 6 will be stored in the variable ans

console.log(Math.floor(6.87));  // The output 6 will go into the log() function

The real strength in functional programming is the ability to write your own functions.

huge chunk of code we call Math