2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244bd
title: Passing Values to Functions with Arguments
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cy8rahW'
2019-07-31 11:32:23 -07:00
forumTopicId: 18254
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
< dfn > Parameters< / dfn > are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or < dfn > "passed"< / dfn > ) into a function when it is called are known as < dfn > arguments< / dfn > .
2020-11-27 19:02:05 +01:00
Here is a function with two parameters, `param1` and `param2` :
2019-05-17 06:20:30 -07:00
```js
function testFun(param1, param2) {
console.log(param1, param2);
}
```
2020-11-27 19:02:05 +01:00
Then we can call `testFun` : `testFun("Hello", "World");` We have passed two arguments, `"Hello"` and `"World"` . Inside the function, `param1` will equal "Hello" and `param2` will equal "World". Note that you could call `testFun` again with different arguments and the parameters would take on the value of the new arguments.
# --instructions--
2018-09-30 23:01:58 +01:00
< ol > < li > Create a function called < code > functionWithArgs< / code > that accepts two arguments and outputs their sum to the dev console.< / li > < li > Call the function with two numbers as arguments.< / li > < / ol >
2020-11-27 19:02:05 +01:00
# --hints--
`functionWithArgs` should be a function.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof functionWithArgs === 'function');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`functionWithArgs(1,2)` should output `3` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
if (typeof functionWithArgs === 'function') {
capture();
functionWithArgs(1, 2);
uncapture();
}
assert(logOutput == 3);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`functionWithArgs(7,9)` should output `16` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
if (typeof functionWithArgs === 'function') {
capture();
functionWithArgs(7, 9);
uncapture();
}
assert(logOutput == 16);
```
You should call `functionWithArgs` with two numbers after you define it.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
/functionWithArgs\([-+]?\d*\.?\d*,[-+]?\d*\.?\d*\)/.test(
code.replace(/\s/g, '')
)
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --before-user-code--
2018-09-30 23:01:58 +01:00
```js
var logOutput = "";
var originalConsole = console
function capture() {
var nativeLog = console.log;
console.log = function (message) {
if(message) logOutput = JSON.stringify(message).trim();
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
}
function uncapture() {
console.log = originalConsole.log;
}
capture();
```
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
uncapture();
if (typeof functionWithArgs !== "function") {
(function() { return "functionWithArgs is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function functionWithArgs(a, b) {
console.log(a + b);
}
functionWithArgs(10, 5);
```