Javascript returning a function

Hello,

I am learning Javascript. I have completed basic understanding of javascript with a lot of practices. I was learning “closures” but I got confused while playing around with this piece of code. Can someone explain this?

function showname(firstname,lastname){
  var nameintro= "Your name is:   ";

  function makefullname()
  {
    console.log(nameintro + firstname + " " + lastname);
  }

  makefullname();
}

console.log(showname("Michael", "Jackson"));

when I run this, I get
Your name is: Michael Jackson
undefined

Then I commented out the //makefullname(); line. and now I just get “undefined”. Where is that coming from?

  1. How I tried this:
function showname(firstname,lastname){
  var nameintro= "Your name is:   ";

  function makefullname()
  {
    return nameintro + firstname + " " + lastname;
  }

   makefullname();
}

console.log(showname("Michael", "Jackson"));

I thought, I am passing the variables to the outer function. Inner function will “return” the concatenated string when that function is called. I still get “undefined”. why is that?

  1. Now I used this code.
function showname(firstname,lastname){
  var nameintro= "Your name is:   ";

  function makefullname()
  {
    return nameintro + firstname + " " + lastname;
  }

   return makefullname();
}

console.log(showname("Michael", "Jackson"));

This works fine. When I return a function from a function, i thought the receiver will get the function definition. Instead it gets the value returned by the innerfunction that has been called. I am really confused.
Can someone explain. Thanks for your time!

Regards,
RM

Fantastic. Thanks a lot for your time!

RM