Creating and calling a function?

I need a bit of code that does this:

If last word is "pen":
     call whatcolor();   [example psudocode]
     If adj is "blue"
         ...
         ...
[and later down]

function watercolor()
      adj is now the second-to-last word;
      return.

I can’t seem to figure out how to make functions. I need to do the same procedure over and over again without wiriting the same code over and over again. How on earth do you do this in Inform?

The simplest way is to use the to decide which … is … phrase.

[code]A color is a kind of value. The colors are red, green, blue, black, white, teal, mauve, and octarine.

To decide which color is the magical color:
decide on octarine.

Home is a room. A gong is here.

Instead of touching the gong: say “You strike the gong, eliciting a pure note and a sparkling haze of [the magical color].”

Test me with “touch gong”.[/code]

You could also do it using Inform 6 includes, but that’s ridiculously hacky for trivial code.

Inform 7’s equivalent to functions are phrases. To define a phrase you use the construct “To name of the phrase:” followed by a tab-indented block of statements to execute. You call the phrase simply by typing it’s name. An example would be:

To do stuff:
    say "Doing stuff". [Pretend the four spaces at the beginning of this line are a tab]

[Somewhere else in the code:]
if some condition:
    do stuff;
    [...]

For your particular use case, I think there’s a better way to achieve your goal than what you’re trying to do. Instead of having a variable that contains the current color and setting that variable using a rule, you could have a rule to determine what the current color is like this:

To decide which text is the current color:
    decide on the second-to-last word. ["decide on" being Inform 7's "return"]

If the current color is "blue":

To find more information about phrases and what they can do (including how to parametrize them, which I haven’t covered at all in the example above), look at chapter 11 of the Inform 7 manual.

This isn’t exactly what I’m looking for. I need to call a function so I can change game variables, I’m not trying to do text substitution.

Here’s the actual code

If last word is "kudasai": 
		Increase friendship by 1;
		let japanese output be "下さい。";
		[I have the Kudasai, now strip off the end, leaving the -te form verb]
		replace word number (last position) in english input with "";
		Let the last position be the number of words in english input;
		Let last word be word number (last position) in english input;
	[Parse the Verb]
	[***
	The Verb will be either in -te or in imparitive form.
	However, some will be a "suru" verb so we need to parse for this first, group and look up
	suru -te form (plain - shite) | imperative (plain - shiro) 
	***]
	If the last word is "shite":
		[delete this word and now grab the new last word and combine]
	If the last word is "shiro"
                decrease friendship by 1;
                [delete this word and now grab the new last word and combine]
	otherwise:
		....

Here’s the problem.

I have a Japanese sentience which is “Kougeki shite kudasai”
The parser will detect if the last word is “kudasai”, process it, and then delete the word at the end of the sentence.

The next step is to see if the last word now is “Shite” or “shiro”, if it’s one of those two then it needs to combine the next word previous and then delete those to parse next last word.

I’m trying to make a function of [delete this word and now grab the new last word], which doesn’t print any output, it just deletes last word from from a string and set a pointer to the new last word of the string.

the actual function is this

		replace word number (last position) in english input with "";
		Let the last position be the number of words in english input;
		Let last word be word number (last position) in english input;
                [and combine with "suru"]

I’m not sure my meaning came across too well. The To decide which … is … phrase does indeed seem to be what you want, since although I used it in text substitution for the example, that was incidental. The “to decide” statement is for all intents and purposes a function. For instance, all that the code

To decide which person is the man in charge: decide on the player.

does is this: as soon as you refer to “the man in charge”, Inform looks at this “to decide” statement to determine the object referred to. You can do the same thing with a value, with indexed text, or most anything else.

Ahh, I found what I was looking for, which is something like that. However, now I have a scope problem.

The keyword I was looking for was “To…”

let japanese output be "下さい。";
chomp;
...
...


To chomp:
		replace word number (last position) in english input with "";
		Let the last position be the number of words in english input;
		Let last word be word number (last position) in english input;

However, it seems that Inform has the concept of scope.

In the sentence 'replace word number (last position) in english input with ""'  , I was expecting to read a number, but instead found some text that I couldn't understand - '(last position)'.

How do you define a global in inform?

Edit :

I tried this

		place last position in scope;
		place english input in scope;

But it (strangely) can’t find the variable to put into scope. Which… I guess, defeats the purpose?

Aha! You can pass a function variables (I thought you could), but it’s note seeing my deceleration.

If last word is "kudasai": 
		Increase friendship by 1;
		let japanese output be "下さい。";
		[I have the Kudasai, now strip off the end, leaving the -te form verb]
		chomp english input at last position;
...
...
To chomp (english input - indexed text) at (last postion - a number ):
		replace word number (last position) in english input with "";
		Let the last position be the number of words in english input;
		Let last word be word number (last position) in english input;

It still can’t see “english input” or “last position”
I’m also going to need to return the english input string when I’m done… I take there isn’t a return(string) command :slight_smile:

Man Inform is great at englishy-like things, but it really sucks at computery-function things… I guess that making it “regular” will defeat the ability to put it into “english”

Foobar is a text [any other kind of value can be used here, too, of course} that varies.

But, as you have already discovered, phrases in Inform 7 can have parameters and global variables should definitely be used as a substitute for them.

It can’t see “last position” because you made a typo in the parameter name: “last pos[sic]tion”. It should be able to see english text just fine.

That should fix the problem of it not being able to see the variables. However you have another problem: The modifications you make to english input inside the chomp phrase will be local - they won’t affect the text variable that you invoked the phrase with.

I think your best bet here is to make chomp a decision (i.e. to give it a return value) that decides on (i.e. returns) the chomped text.

You can have an I7 phrase/function return a value thus:

To decide what indexed text is --/the chomped (sentence - indexed text): replace word number (number of words in sentence) in sentence with ""; decide on the sentence.
This permits you to write code like now the English input is the chomped English input which will calculate the value of the ‘chomped English input’ and put it into the 'English input variable)

Also, I guess your ‘chomp’ phrase creates to new local variables called ‘english input’ and ‘last position’, and that’s why the values of the existing globals ‘english input’ and ‘last position’ aren’t affected.

OP, I don’t know if you’ve run across this yet, but I’ve found the following document to be indispensable for translating traditional programming concepts into Inform 7. It is written referencing an older version, so some things may not be quite current, but I am not sure. In particular, I believe procedural rules are being phased out.

ifwiki.org/index.php/Inform_ … rogrammers

I found that, and it’s helpful, but what would be better is programming examples in C for example, and then the inform syntax that does the same thing. It talks in the context of inform. A perfect example is functions. If the document said this:

it would of been a little more clear.

Also, is there an IRC channel I can hang out on? I feel like I’m spamming these forums :confused:

The closest thing the IF world has to an IRC channel is ifMUD. That said, intfiction.org is not a high-traffic forum, and people here like to help; don’t be too worried about spamming it. (For detailed code questions, it’s also an easier format to use than ifMUD; you can post code here a lot more easily.)

Indeed they are! They’ll be gone by the next release of Inform.