1

This is my first time posting after looking through for a while and I am new to javascript, but I am having trouble figuring this out.

I am trying to call a function I have created, and everytime I attempt to call it...i am told I am not calling the function.

What am I missing?

//Simple greeting function
var greeting = function (name) {
console.log("Great to see you," + " " + name);
};

//Calling function
var greeting = function(name) {
console.log("Hello" + "" + name);
} // this was missing in the original question

or if I try

functionName = function(name)

I'll get a syntax error

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
MusclesGlassez
  • 309
  • 1
  • 2
  • 12

5 Answers5

1

What?! Try this:

var greeting = function(name) {
   console.log('Hello ' + name);
}

greeting('ktm');

So there are two ways to define functions.

(1) Function Expression

var greeting = function(name) { ... }

(2) Function Declaration

function greeting(name) { ... }

There is a simple way to call functions.

greeting('hello');

You can actually pass it any number of arguments, but if the function expects an argument, and you pass it none, it will treat it as undefined.

ktm5124
  • 11,861
  • 21
  • 74
  • 119
  • Thank you. I don't mean to waste anyone's time. I'm new to javascript. – MusclesGlassez Mar 11 '13 at 03:39
  • You're not wasting anyone's time, seriously :-) It was my pleasure to write this response. I'm sorry if you mistook the (initial) jocular presentation as condescending. – ktm5124 Mar 11 '13 at 03:42
1

You're not calling your function at all.

A call to your function would look like this:

greeting( "Mike" );
Michael Geary
  • 28,450
  • 9
  • 65
  • 75
0

Try:

//Simple greeting function
var greeting = function (name) {
    console.log("Great to see you," + " " + name);
};

greeting('foo bar');
Petah
  • 45,477
  • 28
  • 157
  • 213
0

Here is how to define your function:

function greeting(name) {

    console.log("Great to see you," + " " + name);

}

and call it:

greeting("Tomas");
Kilizo
  • 3,192
  • 4
  • 19
  • 20
0

or you can do...

/*here i am declaring my function*/
function greeting(name) {
console.log(name);
}

/*here i am calling it*/
greeting('ktm');

function greeting(){} is a function declaration. Function declarations are probably easier to pick up and learn before you go diving-in, into storing functions into variables. Especially if you're learning this stuff for the first time.

klewis
  • 7,459
  • 15
  • 58
  • 102
  • I think it's not as confusing as it seems. The distinction is such a buzzword that it becomes confusing because you *hear about it* before you actually *learn about it*. The most effective remedy is a concise explanation: http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip – ktm5124 Mar 11 '13 at 03:49
  • I totally agree. But the beauty of learning a function declaration first is that it can be set anywhere on your JavaScript page unlike some other approaches, which is why I recommend as a first learning step, just in case you end up putting referencing code above it or below it. – klewis Mar 11 '13 at 03:58