The null data type (as a parametre of a function) ?????????

  • Maisara-WD
  • Novice
  • Novice
  • Maisara-WD
  • Posts: 15

Post 3+ Months Ago

Hi guys...

I cannot understand how can the null be used as a parametre for a function.. The book I'm using in studying is telling the following:

Code: [ Select ]
The null value can be used as a parameter to a function, to indicate that a parameter is being omitted.


How..?? I mean.. If I want to ommit a parametere from the function, I dn't need to make it pass into it from the beginning????

explain please

thanx
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

That just means, that when you call the function, you can pass null.

Code: [ Select ]
a_function('one', null, 'three');


This especially comes in handy when you want to pass a later argument, but want the behavior for a missing previous argument to happen.

It's a flexibility thing.
  • Maisara-WD
  • Novice
  • Novice
  • Maisara-WD
  • Posts: 15

Post 3+ Months Ago

Thanx man

it's clear now

but for what reason?? why di I pass null into a function.. can u give me an example for a situation we can use it??

thank u
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Assume you have a function designed to take 3 arguments. A person, a place, and a thing. In that order.

Now, this function might have the person doing something in the place with the thing. Which might be called like this.
Code: [ Select ]
act_with (person, place, thing);


It might also have the person doing something in a place, but there's no thing involved. Which might be called like this.
Code: [ Select ]
act_with (person, place, null);


Lastly, it might have the person doing something with a thing, but not have any information about a place. Which might be called like the following.
Code: [ Select ]
act_with (person, null, thing);


The function declaration in these situations would likely require a person to be passed as the first argument, and if the second or third arguments are not null do something with them too.

Let's assume the person, place, and thing function is an attack function for a game. The function would obviously require a person to carry out the attack, and a place for the attack to happen, but the person might not have a thing to attack with.

In that situation, the attack function might set the attacker to person, and modify persons strength attributes depending on place, and if there's a thing passed instead of null for the third argument it may modify the attackers power according to the attributes of thing. However if thing is null, it would just use the a default such as bare hands.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 10 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.