Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Listing: “hello world” in Greek
```c

main()
printf "‚„ƒ†…˙ ‡‰ˆ‹ŠŒŽ ˙Љˆ‰‹Š„‘\n"
printf "Γειάσου κόσμος\n"

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ strupper(string[])
```c

main()
printf "˙ ˙\n"
printf "Γειάσου κόσμος\n"

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ expression, the function will get executed in that statement/expression.
The statement that refers to the function is the “caller” and the function itself,
at that point, is the “callee”: the one being called.

The standard syntax for calling a function is to write the
The standard syntax for calling a function is to write the

### • Chamando funções

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`:
Expand Down Expand Up @@ -620,8 +621,9 @@ Programas standalone precisam ter `main` (sem parâmetros), ponto de partida da

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.

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.
Latin-1 character set) by the “hard space” code in the ANSI character table:
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
onkey abaixo substituiria cada caractere “~” (código 126 no conjunto
de caracteres ISO Latin‑1) pelo código de “espaço duro” na tabela ANSI:

Listing: onkey function

Expand Down Expand Up @@ -1082,9 +1084,13 @@ parser will attempt to use the user-defined operator rather than the default
`User-defined operators: 86`

---

### • Call by Value and Call by Reference

In Pawn, function arguments can be passed in two ways: by value and by reference.

#### Call by value

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.

```c
Expand All @@ -1101,14 +1107,18 @@ main(){
printf("The value of x is %d and value of y is %d, after calling 'swap'.", x, y);
}
```

Output

```
The value of x is 10 and value of y is 20, before calling 'swap'.
The value of x is 10 and value of y is 20, after calling 'swap'.
```

#### Call by reference

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.

```c
swap(&a, &b){
new c = a;
Expand All @@ -1123,19 +1133,28 @@ main(){
printf("The value of x is %d and value of y is %d, after calling 'swap'.", x, y);
}
```

Output

```
The value of x is 10 and value of y is 20, before calling 'swap'.
The value of x is 20 and value of y is 10, after calling 'swap'.
```

### • Recursion / Function Recursion

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.
##### Base Case:

##### Base Case:

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.

##### Recursive Case:

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.

#### Example

```c
stock factorial(n) {
// Base case: factorial of 0 is 1
Expand All @@ -1153,7 +1172,9 @@ main() {
printf("Factorial of %d is %d", num, result); // Output: Factorial of 3 is 6
}
```

#### Demonstrate the Output

```
main() \\ main function from where execution of program starts
new num = 3; \\ creates a num variable
Expand All @@ -1169,11 +1190,15 @@ factorial(3) \\ factorial initiate
else{ 1 * factorial(1-1) } \\ 3 * 2 * 1 and calls the factorial(0)
factorial(0) \\ factorial initiate again
if(0 == 0) return 1 \\ checks the conition which is true and return 1
\\ at the final call 3 * 2 * 1 * 1
\\ at the final call 3 * 2 * 1 * 1
```

### Stack Memory

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.

#### Example (Stack Overflow)

```c
#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
main(){
Expand All @@ -1184,14 +1209,17 @@ grow_stacK(n){ // recursive function
grow_stacK(n+1);
}
```

#### Output

```
N: 1
N: 2
N: 3
.. .
Stack/heap collision (insufficient stack size)
```

![Stack](https://i.imgur.com/ZaIVUkJ.png)

[Go Back to Contents](00-Contents)