19_functions2.zig 905 B

12345678910111213141516171819202122232425262728293031
  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. //
  20. // Please give this function the correct input parameter(s).
  21. // You'll need to figure out the parameter name and type that we're
  22. // expecting. The output type has already been specified for you.
  23. //
  24. fn twoToThe(???) u32 {
  25. return std.math.pow(u32, 2, my_number);
  26. // std.math.pow(type, a, b) takes a numeric type and two numbers
  27. // of that type and returns "a to the power of b" as that same
  28. // numeric type.
  29. }