-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: handle early return in function #9
- Loading branch information
Showing
14 changed files
with
170 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,43 @@ | ||
PROG | ||
|
||
var i : int; | ||
var n : int; | ||
var fib : int; | ||
var fib_1 : int; | ||
var fib_2 : int; | ||
|
||
for (i := 5; i > 0; i--) { | ||
print("i=%d\n", i); | ||
|
||
func fibonacci(num : int) -> int { | ||
var i: int; | ||
if (num <= 1) { return num; } | ||
|
||
fib_1 := 1; | ||
fib_2 := 0; | ||
|
||
for (i := 2; i <= num; i++) { | ||
fib := fib_1 + fib_2; | ||
fib_2 := fib_1; | ||
fib_1 := fib; | ||
} | ||
|
||
return fib_1; | ||
} | ||
|
||
print("--> i=%d\n", i); | ||
|
||
i++; | ||
proc display_fibonacci(num : int, result : int) { | ||
print("Fibonacci number at position %d is %d\n", num, result); | ||
} | ||
|
||
proc main() { | ||
print("Enter the position in Fibonacci sequence: "); | ||
input("%d", n); | ||
|
||
|
||
if (n < 0) { | ||
print("Fibonacci is undefined for negative numbers.\n"); | ||
} else { | ||
fib := fibonacci(n); | ||
display_fibonacci(n, fib); | ||
} | ||
} | ||
|
||
print("--> i=%d\n", i); | ||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
PROG | ||
|
||
var n : int; | ||
var fib : int; | ||
var fib_1 : int; | ||
var fib_2 : int; | ||
|
||
|
||
func fibonacci(num : int) -> int { | ||
var i: int; | ||
if (num <= 1) { return num; } | ||
|
||
fib_1 := 1; | ||
fib_2 := 0; | ||
|
||
for (i := 2; i <= num; i++) { | ||
fib := fib_1 + fib_2; | ||
fib_2 := fib_1; | ||
fib_1 := fib; | ||
} | ||
|
||
return fib_1; | ||
} | ||
|
||
|
||
proc display_fibonacci(num : int, result : int) { | ||
print("Fibonacci number at position %d is %d\n", num, result); | ||
} | ||
|
||
proc main() { | ||
print("Enter the position in Fibonacci sequence: "); | ||
input("%d", n); | ||
|
||
|
||
if (n < 0) { | ||
print("Fibonacci is undefined for negative numbers.\n"); | ||
} else { | ||
fib := fibonacci(n); | ||
display_fibonacci(n, fib); | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
PROG | ||
|
||
func main(n: int) -> int { | ||
if (n < 1) { | ||
return 0; | ||
} | ||
|
||
return main(n - 1); | ||
} | ||
|
||
print("%d\n", main(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters