Jump to content

Having A Hard Time Understanding Function Parateters


madsovenielsen
 Share

Recommended Posts

Hello all.Im having a hard time understanding function parameters. what are the applications for it. and how does it work ?

function helloworld(string){	return "hello world";}

I know theres probably some place i can find it. but im hoping one from the forum can explain it to me. /mads

Link to comment
Share on other sites

Hello all.Im having a hard time understanding function parameters. what are the applications for it. and how does it work ?
function helloworld(string){	return "hello world";}

I know theres probably some place i can find it. but im hoping one from the forum can explain it to me. /mads

function writeGreatings(justAname){document.write("Hello" justAname);}var user = "Carl";writeGreatings(user);output:Hello Carl

hope this helps. And correct me if I'm wrong.

Edited by ckrudelux
Link to comment
Share on other sites

You should probably read the JavaScript tutorialhttp://www.w3schools.com/js/default.aspa few times over and run through some of the 'try it yourself' examples.The short answer is that a function parameter is an input variable, some value you pass to a function that the function may then act upon.When you alert('hello world!'); 'hello world!' is the parameter you are passing to the alert() function.Practice makes for better understanding.

Link to comment
Share on other sites

function writeGreatings(justAname){document.write("Hello" justAname);}var user = "Carl";writeGreatings(user);output:Hello Carl

hope this helps. And correct me if I'm wrong.

Yes. but im still not sure i understand the benefits. i mean you can get the exact same result with a variable.
Link to comment
Share on other sites

You can use anything as a function parameter. You can send a function as a parameter to another function. You can send objects, arrays, functions, events, regular variables, constants, etc. The reason you use parameters is so that you can build a function that you can use in several situations. You can only use this function in one situation, to show an error that they need to fill out a username:

function show_error(){  alert('Username is empty');}

That's not very useful. If you send the text as a parameter then you can use it to show anything:

function show_error(txt){  alert(txt);}

Granted, a function that just calls alert isn't very useful, but you should get the idea.Maybe an add function is more useful:

function add(a, b){  return a + b;}

Edited by justsomeguy
Link to comment
Share on other sites

When you call a function you write parentheses after the name, parameters go in the parentheses. The alert function takes 1 parameter:

// send constantalert('text');// send variablevar txt = 'show this';alert(txt);// send return value from another functionfunction get_text(){  return 'some text';}alert(get_text());// send value of an expressionalert(1 + 2);alert(typeof window.document);alert(typeof document.getElementById);alert(1 + 2 == 3);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...