I don't understand what purpose the function "name" has here


#1

var nameString = function (name) {
return "Hi, I am"+" "+name;
}
console.log(nameString("Trey"));

My code works but I don't know why

I understand what calling nameString does in the console, but what does (name) do if i have to type my name in anyway?


#2

Hi

The purpose of a function is to be avoid to write the same code many times. Now that you have your function, you can call it with different values for name, for example :

var nameString = function (name) {
   return "Hi, I am"+" "+name;
}
console.log(nameString("Trey"));
console.log(nameString("Adam"));
console.log(nameString("Charlotte"));
console.log(nameString("Yvan"));
console.log(nameString("Maxime"));
console.log(nameString("Bernard"));

I agree with you that this function is not really interesting but you could imagine a function which contains a lot of lines. With this function, you don't have to write again all these lines because the value of a parameter has changed. For example :

var myFunction = function(variable) {
 # ...
 # 50 lines of code
 # ...
  return something
}
var value1 = myFunction(50) ;
var value2 = myFunction(520) ;
var value3 = myFunction(10) ;

In this example, I have a function with 50 lines of code. I don't have to write again these 50 lines of code if I want to evaluate this function with different values of the variable (here 50, 520 and 10). Without this function, I would have to write 150 lines of codes for these 3 evaluations, and this is somewhat uncomfortable to read.

I hope that the utility of functions is now clearer :slight_smile:

Cheerio


#3

Hey, thanks for the reply!

I understand the point of a function and it's use but it's just that in my example I'm confused about the purpose of name. How is name being implemented in the code? I feel like i'm missing something crucial to understanding what is being processed by the computer. How does name play into the line

console.log(nameString("Trey")); - what is name doing in this line? nameString is returning "Hi, I am" and then my name in the console. I'm just confused about what calling function (name) actually does.


#4

sorry that I didn't understand your question the first time.

The variable name is local to the function nameString(). Local means that name can not be accessed outside of the function. When you don't call your function, python see that there is a function called nameString() which receives name as a parameter and do something with it. At this point, it is not important to know what name is. It could be anything (like an integer, an object, an array, ...)!
Then, when you call your function with the line nameString("Trey"), you specify that the local variable name takes the value "Trey". Then python understands that, in the function, name is a string and not anything else (integer, object, ...). Now that python knows that name is a string, it can apply the string concatenation rule "Hi, I am"+" "+name; to build a longer string which contains "Hi, I am", then a white space " ", then the name. This longer string can then be printed by the console.log().


#5

Ah, thank you! I think that I understand it now!