You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: frontend/i18n/pt-BR/docusaurus-plugin-content-docs/current/scripting/language/reference/04-Functions.md
+33-5Lines changed: 33 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,7 +173,8 @@ expression, the function will get executed in that statement/expression.
173
173
The statement that refers to the function is the “caller” and the function itself,
174
174
at that point, is the “callee”: the one being called.
175
175
176
-
The standard syntax for calling a function is to write the
176
+
The standard syntax for calling a function is to write the
177
+
177
178
### • Chamando funções
178
179
179
180
Para chamar uma função, escreva o nome seguido da lista de parâmetros entre parênteses. Mesmo que não haja argumentos, os parênteses permanecem. O exemplo below mostra como chamar `power`:
@@ -620,8 +621,9 @@ Programas standalone precisam ter `main` (sem parâmetros), ponto de partida da
620
621
621
622
Bibliotecas não exigem `main`, mas devem possuir pelo menos uma função pública. `main` é o entry point primário; funções `public` fornecem pontos alternativos. A VM pode iniciar por uma função pública e, em bibliotecas, `main` pode servir para inicializações.
622
623
623
-
Para declarar uma função pública, prefixe o nome com `public`. Exemplo: um editor de texto poderia chamar `onkey` a cada tecla digitada para que o script modifique/rejeite caracteres.
624
-
Latin-1 character set) by the “hard space” code in the ANSI character table:
624
+
Para declarar uma função pública, prefixe o nome com `public`. Exemplo: um editor de texto poderia chamar `onkey` a cada tecla digitada para que o script modifique/rejeite caracteres. A função
625
+
onkey abaixo substituiria cada caractere “~” (código 126 no conjunto
626
+
de caracteres ISO Latin‑1) pelo código de “espaço duro” na tabela ANSI:
625
627
626
628
Listing: onkey function
627
629
@@ -1082,9 +1084,13 @@ parser will attempt to use the user-defined operator rather than the default
1082
1084
`User-defined operators: 86`
1083
1085
1084
1086
---
1087
+
1085
1088
### • Call by Value and Call by Reference
1089
+
1086
1090
In Pawn, function arguments can be passed in two ways: by value and by reference.
1091
+
1087
1092
#### Call by value
1093
+
1088
1094
In this method, the value of the variable is passed to the function. A copy of the variable is created and the function operates on the copy, not the original variable. Any changes made to the variable inside the function do not affect the original variable.
1089
1095
1090
1096
```c
@@ -1101,14 +1107,18 @@ main(){
1101
1107
printf("The value of x is %d and value of y is %d, after calling 'swap'.", x, y);
1102
1108
}
1103
1109
```
1110
+
1104
1111
Output
1112
+
1105
1113
```
1106
1114
The value of x is 10 and value of y is 20, before calling 'swap'.
1107
1115
The value of x is 10 and value of y is 20, after calling 'swap'.
1108
1116
```
1109
1117
1110
1118
#### Call by reference
1119
+
1111
1120
In this method, the address of the variable is passed to the function. The function operates on the original variable and any changes made to the variable inside the function are reflected in the original variable.
1121
+
1112
1122
```c
1113
1123
swap(&a, &b){
1114
1124
new c = a;
@@ -1123,19 +1133,28 @@ main(){
1123
1133
printf("The value of x is %d and value of y is %d, after calling 'swap'.", x, y);
1124
1134
}
1125
1135
```
1136
+
1126
1137
Output
1138
+
1127
1139
```
1128
1140
The value of x is 10 and value of y is 20, before calling 'swap'.
1129
1141
The value of x is 20 and value of y is 10, after calling 'swap'.
1130
1142
```
1131
1143
1132
1144
### • Recursion / Function Recursion
1145
+
1133
1146
Recursion in programming refers to the process of a function calling itself in order to solve a problem. It's a fundamental concept used to solve problems that can be broken down into smaller instances of the same problem. Recursion consists of two main components: base cases and recursive cases.
1134
-
##### Base Case:
1147
+
1148
+
##### Base Case:
1149
+
1135
1150
Every recursive function should have one or more base cases. A base case is a condition under which the function stops calling itself and returns a result directly. Without base cases, the recursion would continue indefinitely, causing a stack overflow. Read Stack/Heap section to know more about it.
1151
+
1136
1152
##### Recursive Case:
1153
+
1137
1154
The recursive case is where the function calls itself to solve a smaller instance of the problem. Each recursive call should bring the problem closer to a base case.
1155
+
1138
1156
#### Example
1157
+
1139
1158
```c
1140
1159
stock factorial(n) {
1141
1160
// Base case: factorial of 0 is 1
@@ -1153,7 +1172,9 @@ main() {
1153
1172
printf("Factorial of %d is %d", num, result); // Output: Factorial of 3 is 6
1154
1173
}
1155
1174
```
1175
+
1156
1176
#### Demonstrate the Output
1177
+
1157
1178
```
1158
1179
main() \\ main function from where execution of program starts
else{ 1 * factorial(1-1) } \\ 3 * 2 * 1 and calls the factorial(0)
1170
1191
factorial(0) \\ factorial initiate again
1171
1192
if(0 == 0) return 1 \\ checks the conition which is true and return 1
1172
-
\\ at the final call 3 * 2 * 1 * 1
1193
+
\\ at the final call 3 * 2 * 1 * 1
1173
1194
```
1195
+
1174
1196
### Stack Memory
1197
+
1175
1198
The stack is a region of memory used for storing local variables, function call information, and control flow data. It operates in a Last-In-First-Out (LIFO) manner, which means that the last item pushed onto the stack is the first one to be popped off.
1199
+
1176
1200
#### Example (Stack Overflow)
1201
+
1177
1202
```c
1178
1203
#pragma dynamic 35 // (35 * 4 bytes, a cell size) #pragma dynamic [cells] helps to modify the size of stack, read docs/scripting/language/Directives to know more about #pragma
1179
1204
main(){
@@ -1184,14 +1209,17 @@ grow_stacK(n){ // recursive function
0 commit comments