019_functions2.zig 932 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // Now let's create a function that takes a parameter. Here's an
  3. // example that takes two parameters. As you can see, parameters
  4. // are declared just like any other types ("name": "type"):
  5. //
  6. // fn myFunction(number: u8, is_lucky: bool) {
  7. // ...
  8. // }
  9. //
  10. const std = @import("std");
  11. pub fn main() void {
  12. std.debug.print("Powers of two: {} {} {} {}\n", .{
  13. twoToThe(1),
  14. twoToThe(2),
  15. twoToThe(3),
  16. twoToThe(4),
  17. });
  18. }
  19. // Please give this function the correct input parameter(s).
  20. // You'll need to figure out the parameter name and type that we're
  21. // expecting. The output type has already been specified for you.
  22. //
  23. fn twoToThe(???) u32 {
  24. return std.math.pow(u32, 2, my_number);
  25. // std.math.pow(type, a, b) takes a numeric type and two
  26. // numbers of that type (or that can coerce to that type) and
  27. // returns "a to the power of b" as that same numeric type.
  28. }