From 08ec029f20381580ebe76ad8bd3feca2e5cd262a Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Fri, 22 Jan 2021 17:42:03 -0500 Subject: Added ex 19,20 functions and pop quiz --- 18_functions.zig | 6 +++++- 19_functions2.zig | 28 ++++++++++++++++++++++++++++ 20_quiz3.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++ README.md | 4 ++-- ziglings | 3 ++- 5 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 19_functions2.zig create mode 100644 20_quiz3.zig diff --git a/18_functions.zig b/18_functions.zig index ad16a13..ad97585 100644 --- a/18_functions.zig +++ b/18_functions.zig @@ -12,7 +12,11 @@ pub fn main() void { // // We're just missing a couple things here. One thing we're NOT missing is the -// keyword 'pub', which is not needed here. Can you guess why? +// keyword "pub", which is not needed here. Can you guess why? +// +// Functions need to specify the type of value they return. The main() function +// above has a special return type "void", which means it returns nothing. This +// function returns something. What might that be? // ??? deepThought() ??? { return 42; // Number courtesy Douglas Adams diff --git a/19_functions2.zig b/19_functions2.zig new file mode 100644 index 0000000..68cc67b --- /dev/null +++ b/19_functions2.zig @@ -0,0 +1,28 @@ +// +// Now let's use a function that takes a parameter. +// +const std = @import( "std" ); + +pub fn main() void { + std.debug.print("Powers of two: {} {} {} {}\n", .{ + twoToThe(1), + twoToThe(2), + twoToThe(3), + twoToThe(4), + }); +} + +// +// Oops! We seem to have forgotten something here. Function +// parameters look like this: +// +// fn myFunction( number: u8, is_lucky: bool ) { +// ... +// } +// +// As you can see, we declare the type of the parameter, just +// like we declare the types of variables, with a colon ":". +// +fn twoToThe(???) u32 { + return std.math.pow(u32, 2, my_number); +} diff --git a/20_quiz3.zig b/20_quiz3.zig new file mode 100644 index 0000000..e18ef37 --- /dev/null +++ b/20_quiz3.zig @@ -0,0 +1,45 @@ +// +// Let's see if we can make use of some of things we've learned so far. +// We'll create two functions: one that contains a "for" loop and one +// that contains a "while" loop. +// +// Both of these are simply labeled "loop" below. +// +const std = @import( "std" ); + +pub fn main() void { + const my_numbers = [4]u16{ 5,6,7,8 }; + + printPowersOfTwo(my_numbers); + std.debug.print("\n", .{}); +} + +// +// You won't see this every day: a function that takes an array with +// exactly four u16 numbers. This is not how you would normally pass +// an array to a function. We'll learn about slices and pointers in +// a little while. For now, we're using what we know. +// +// This function prints, but does not return anything. +// +fn printPowersOfTwo(numbers: [4]u16) ??? { + loop (numbers) |n| { + std.debug.print("{} ", .{twoToThe(n)}); + } +} + +// +// This function bears a striking resemblance to twoToThe() in the last +// exercise. But don't be fooled! This one does the math without the aid +// of the standard library! +// +fn twoToThe(number: u16) ??? { + var n: u16 = 0; + var total: u16 = 1; + + loop (n < number) : (n += 1) { + total *= 2; + } + + return ???; +} diff --git a/README.md b/README.md index 4f87789..fe7c628 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,9 @@ Planned exercises: * [x] If * [x] While * [x] For -* [ ] Functions -* [ ] Defer +* [x] Functions * [ ] Errors +* [ ] Defer * [ ] Switch * [ ] Runtime safety * [ ] Unreachable diff --git a/ziglings b/ziglings index fcbcb01..b1caa5c 100755 --- a/ziglings +++ b/ziglings @@ -68,7 +68,6 @@ function check_it { fi } - check_it 01_hello.zig "Hello world" "Note the error: the source file has a hint for fixing 'main'." check_it 02_std.zig "Standard Library" check_it 03_assignment.zig "55 314159 -11" "There are three mistakes in this one!" @@ -87,6 +86,8 @@ check_it 15_for.zig "A Dramatic Story: :-) :-) :-( :-| :-) The End." check_it 16_for2.zig "13" check_it 17_quiz2.zig "8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16" "This is a famous game!" check_it 18_functions.zig "Question: 42" "Can you help write the function?" +check_it 19_functions2.zig "2 4 8 16" +check_it 20_quiz3.zig "32 64 128 256" "Unexpected pop quiz! Help!" echo echo " __ __ _ " -- cgit v1.2.3-ZIG