TDDD34 – Lecture 3 1/7 The grid program: N = input('Enter a number: '); for Row = 1:N for Col = 1:N fprintf('+--'); end fprintf('+\n'); for Col = 1:N fprintf('| '); end fprintf('|\n'); end for Col = 1:N fprintf('+--'); end fprintf('+\n');
TDDD34 – Lecture 3 2/7 Functions No parameters and no return values: function name_of_function statements end One parameter, no return values: function name_of_function(name_of_param) statements end Several parameters, no return values: function name_of_function(param1, param2, param3) statements end
TDDD34 – Lecture 3 3/7 No parameters, one return value: function ret = name_of_function statements end No parameters, several return values: function [ret1 ret2 ret3] = name_of_function statements end Combine the above as you like! For example: function [ret1 ret2] = my_function(param1, param2) statements end Note: You do not have to end your functions with an end .
TDDD34 – Lecture 3 4/7 Return Can be used to terminate a function before that function's end. Example: The function ”max” could be written: function greatest_number = max(a, b) if a > b greatest_number = a; return; end greatest_number = b; end Instead of: function greatest_number = max(a, b) if a > b greatest_number = a; else greatest_number = b; end end
TDDD34 – Lecture 3 5/7 Why Use Functions? Avoid code duplication. Keep code size at manageable level. ✔ Recycling of code: some sub-problems appear frequently. ✔ ”Divide and Conquer”. ✔
TDDD34 – Lecture 3 6/7 Heat oven to 350 degrees F. Grease two 9 inch, round, cake pans. Flour the cake pans. Cover the bottoms of the cake pans with waxed paper. Get a large bowl. Put 2 1/8 cup of flour in the large bowl. Put 2 cups of sugar in the large bowl. Put 3/4 cup of cocoa in the large bowl. Put 1 1/2 teaspoon of baking powder in the large bowl. Put 3/4 teaspoon of baking soda in the large bowl. Put 3/4 teaspoon of salt in the large bowl. Mix the ingredients in the large bowl. Put 3 eggs in the large bowl. Put 1 cup of milk in the large bowl. Put 1/2 cup of vegetable oil in the large bowl. Put 1 tablespoon vanilla in the large bowl. Beat the ingredients in the large bowl until well blended. Pour batter from the large bowl into the prepared pans. Bake for 35 minutes, or until wooden toothpick inserted in centers comes out clean Cool layers in pans on wire racks 10 minutes. Loosen edges of the layers and remove to racks to cool completely. Drain 2 cans of cherries, reserving 1/2 cup juice. Get a 2 quart saucepans. Put the juice in the saucepan. Put the cherries in the saucepan. Put 1 cup of sugar in the saucepan. Put 1/4 cup of constarch in the saucepan. Cook over low heat until thickened, stirring constantly. Stir in 1 teaspoon vanilla in the saucepan. Cool the saucepan Get a chilled medium bowl. Put 3 cups of heavy whipping cream in the medium bowl. Put 1/3 cup confectioners' sugar in the medium bowl. Beat the ingredients in the medium boal until stiff peaks form. Split each cake layer horizontally in half with long serrated knife. Tear one split layer into crumbs, set them aside. Reserve 1 1/2 cups Frosting for decorating cake; set it aside. Gently brush loose crumbs off top and side of each cake layer with pasty brush or hands. Place one cake layer on cake plate. Spread the layer with 1 cup frosting. Top the layer with 3/4 cup cherry topping. Top with second cake layer; repeat layers of frosting and cherry topping. Top with third cake layer. Frost side of cake. Pat reserved crumbs onto frosting on side of cake. Spoon reserved frosting into pastry bag fitted with star decorator tip. Pipe around top and bottom edges of cake. Spoon remaining cherry topping onto top of cake.
TDDD34 – Lecture 3 7/7 After applying ”Divide and Conquer”-strategy: Major steps: I. Bake cake layers. II. Bring out the cherries. III. Make frosting (we need cherry juice for this). IV. Assemble and decorate cake. Further division of major step I: 1. Heat the oven to 350 degrees F. 2. Prepare two pans. 3. Mix dry ingredients in large boal. 4. Add wet ingrediants, beat until well blended. 5. Put batter in the pans. 6. Put pans in the oven for 35 minutes. 7. Let cool. Further division of 2 (1 is trivial): a. Grease two 9 inch, round, cake pans. b. Flour the cake pans. c. Cover the bottoms of the cake pans with waxed paper. … and so on...
Recommend
More recommend