018_functions.zig 925 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // Functions! We've already seen a lot of one called "main()". Now let's try
  3. // writing one of our own:
  4. //
  5. // fn foo(n: u8) u8 {
  6. // return n + 1;
  7. // }
  8. //
  9. // The foo() function above takes a number "n" and returns a number that is
  10. // larger by one.
  11. //
  12. // If your function doesn't take any parameters and doesn't return anything,
  13. // it would be defined like main():
  14. //
  15. // fn foo() void { }
  16. //
  17. const std = @import("std");
  18. pub fn main() void {
  19. // The new function deepThought() should return the number 42. See below.
  20. const answer: u8 = deepThought();
  21. std.debug.print("Answer to the Ultimate Question: {}\n", .{answer});
  22. }
  23. // Please define the deepThought() function below.
  24. //
  25. // We're just missing a couple things. One thing we're NOT missing is the
  26. // keyword "pub", which is not needed here. Can you guess why?
  27. //
  28. ??? deepThought() ??? {
  29. return 42; // Number courtesy Douglas Adams
  30. }