I am having trouble with function parameters

Griffin Guy

I was doing a course on Udemy to learn JavaScript by making a pong game but when I came across this part I was really confused. What do the parameters do? Are they variables? If they are variables can they be accessed out of the function? I just don't understand! Please explain it to me! :(

I don't even understand what he's trying to do!

I think he was trying to draw or fill it in or something.

this is the code:

function colorCircle(centerX, centerY, radius, drawColor) {
    canvasContext.fillStyle = drawColor;
    canvasContext.beginPath();
    canvasContext.arc(centerX, centerY, radius, 0,Math.PI*2, true);
}
Sean Vieira

A variable is simply a named value. Instead of hard-coding values everywhere:

100 /* speed of my canary */ + 10 /* current speed bonus */

we can "name" our values and pass them around (and even change them as we need to without re-writing the rest of our program):

var speedOfMyCanary = 100;
var speedBonus = 10;

speedOfMyCanary + speedBonus // still 110;

A function parameter is simply a variable scoped to a function:

function doWork(a, b, c) {
  // Do work here later with a, b, and c.
}

is the same (conceptually) as:

var a, b, c;
function doWork() {
  // Do work here later with a and b and c
}
// Call do work now
doWork();
delete a;
delete b;
delete c;

if we could carry around the var a, b, c and delete a, b, c with our doWork without copy-pasting it.

That is to say, when we call:

doWork(1, 2, 3);

It is (again, conceptually) the same as:

var a = 1, b = 2, c = 3;
function doWork() {
  // Do work here later with a and b and c
}
doWork();
delete a;
delete b;
delete c;

Function parameters get us a few extra benefits:

First, the parameters a, b, and c are not accessible to code outside our function:

function doWork(a, b, c) {
  // a, b, and c are all accessible here
  return a + b + c;
}

// But we can't access those values out here
// if we try we'll get:
a // ReferenceError - `a` is not defined

The bonus benefit is that our callers don't have to worry about what we call our inputs internally - if we change the names from a, b, and c to x, y, and z our calling code remains unchanged:

function doWork(x, y, z) {}
doWork(1, 2, 3)  // STILL WORKS :-)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am having trouble with the .compareTo function

Typescript - I am having a trouble with using a return value from a function

I Am Having Trouble Executing a list as a string for an automated function

I wrote a function for encrypting an input, but I am having trouble with a decryption function

I am having trouble updating node and npm

I am having trouble with HTML Javascript inclusion

I am having trouble as a newbie with C++

I am having trouble understanding the flow of programming

I am having trouble with ASYNCTASK in my fragment

I am having trouble with my route in laravel

I am having trouble with this piece of code

I am having trouble upcasing or capitalizing

I am having trouble with my Calculator

I am having trouble understanding IBM procedures

Why am I having trouble writing to an array

I am having trouble running my app

I am having a trouble importing the PyDictionary library

I am having trouble with OpenMP on C

I am having trouble getting the below fixed

I am having trouble organising boxes in css

I am having trouble with the move-item function to move multiple files into separate folders

I am having trouble with this error (-215:Assertion failed) !ssize.empty() in function 'resize' in opencv

I am having trouble in displaying date beside the entered todo element. What is wrong with changeDate() function?

I am having trouble using an operator= overload with a static member function. Is there something wrong in the code?

I am having trouble passing a multidimensional variable array to a function in C using malloc()

Using React, I'm having trouble sending a function with parameters to one of my components

I am a beginner and I'm having trouble compiling a simple program

I'm having trouble with my handleCheckBox function

I'm having trouble troubleshooting my function