What should I be doing in place of a function prototype?

dj_d

Member
I'd like to call a function on line X that doesn't get defined until farther down. In C, I'd start with a function prototype and all would be happy. I can't figure out how to make this work in ASH. Help?
 

jasonharper

Developer
It's basically the same as C; a forward declaration is the same as the actual definition, except that you use a semicolon instead of the function body. Here's a rearranged version of some code I'd posted in another thread:
Code:
string join(string joiner, string[int] pieces);

print(join(", ", "lions/tigers/bears/oh my!".split_string("/")));

string join(string joiner, string[int] pieces) {
	buffer buf;
	boolean first = true;
	foreach index in pieces {
		if(!first) buf.append(joiner);
		first = false;
		buf.append(pieces[index]);
	}
	return buf.to_string();
}
ASH does not currently behave very well if you forget to actually define the function, so I'd suggest using this only in the case of mutually recursive functions, the only time when you can't simply move the function definition up above where it's called.
 
Top