A few questions on functions

Post » Sun Jun 17, 2012 10:18 pm

Papyrus is very clever and interesting from what I've seen so far.

However in the CK Wiki there are two different examples of a basic function:
int Function AddTwo(int a, int B) global  return a + bendFunction
and
Function IncrementValue(int howMuch = 1)  myValue += howMuchendFunction
A few questions based on these examples.

1. Why has the first function been defined as holding an integer, when the second one hasn't (yet still contains only integers)?
2. Why isn't the second function defined as a global function?
3. Is there an example of a native function?

Cheers guys :)
User avatar
loste juliana
 
Posts: 3417
Joined: Sun Mar 18, 2007 7:37 pm

Post » Sun Jun 17, 2012 6:16 pm

In the first example, the "int" before "Function" tells the compiler that this function will return an integer. I don't know what "global" does.
In the second example, the function doesn't return a value, so there is nothing before "Function".

someVariable = AddTwo( 1, 2 )

; this is probably an error, because this function hasn't been declared to return anything
anotherVariable = IncrementValue( 42 )

; this should be fine
IncrementValue( 42 )
User avatar
Kaylee Campbell
 
Posts: 3463
Joined: Mon Mar 05, 2007 11:17 am

Post » Sun Jun 17, 2012 9:44 pm

Native functions aren't written in Papyrus, they're only defined. They have the "native" keyword at the end of their declaration. I expect you'll never need to write a declaration, which is why it's not shown on that page.

I'm assuming that the "IncrementValue" function there is acting on a property or variable ("myValue") already declared in the script. This is one reason, at least, why it shouldn't be specified as being global. The type of "myValue" hasn't been specified, but you should note that it could be a "float".

Because it doesn't have a return value, it will not be able to be used as part of an expression, as araneldon2 mentioned. Because "AddTwo" does have a return value, it can be used in expressions in the same way as you could use a variable of type "int".

Cipscis
User avatar
Emerald Dreams
 
Posts: 3376
Joined: Sun Jan 07, 2007 2:52 pm

Post » Sun Jun 17, 2012 9:56 pm

Awesome, cheers guys! :)
User avatar
Sammie LM
 
Posts: 3424
Joined: Thu Nov 30, 2006 1:59 pm

Post » Sun Jun 17, 2012 7:58 pm

“Global” means that the function doesn’t need to run on an in-game object, you can run it on its own (which means it cannot access any properties or object variables, just things that are given to it via parameters). Also, all “native” functions are implemented inside the game, so you won’t be able to create your own.
User avatar
louise tagg
 
Posts: 3394
Joined: Sun Aug 06, 2006 8:32 am


Return to V - Skyrim