Lecture 6 Reminder: Programming Assignment 2 is due by Wednesday by 4:30pm. Log into Windows/ACENET Start Microsoft Visual Studio 2010 and open "CircumferenceProgram" project. Start MS VS again and open "HelloWorldProgram" project. Questions? Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 1 Outline Writing methods Default (value) parameters Returning results Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 2 Methods So far, all of our code has been written in the Main( ) method. Why write our own methods? Organizes code for readability by giving names to computational ideas. E.g., ComputeCircumference Allows reuse so that code does not need to be repeated. E.g. ComputeBallSpeed for with Earth's and the Moon's gravity constant. Easier to prove that the computation is correct. Prove that an individual method is correct, then that the composition of method uses is correct. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 3 Methods x y f(x, y, z, ...) z . . . Recall that a method is used to encapsulate and name a computational idea and can be viewed as a black box that receives data and returns an unnamed result. C# code must describe these parts. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 4 Writing Methods A C# method is a subprogram that is written inside a class (but outside of the Main( ) method). It has the following syntax: [<modifier(s)>] <returnType> <name> (<parameterList>) { <body of method> } The first line is called the method heading. Modifiers determine what kind of method is being defined and are optional. Today, we need static. We'll see other modifiers later. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 5 Writing Methods returnType is the type of the result value. name is the name of the method. parameterList is a comma-separated list of parameter declarations. A default parameter declaration is just like a variable declaration: <type> <varName> and gives (local) names to the received data. body of a method contains the declarations and executable statements to compute the result. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 6 Default Parameters public static void Main() { int x = 3, y; x Console.WriteLine(x); y = f(x); Console.WriteLine(x); ... } argument 3 37 static int f (int r) { r Console.WriteLine(r); r = 7; Console.WriteLine(r); ... } parameter When a method is called, a default parameter is initialized with a copy of the corresponding actual argument. A change to the parameter is not reflected in the argument. The example above displays 3, 3, 7, 3. A corresponding argument to a default parameter may be any expression. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 7 Returning Results A method may communicate a result to the method caller by returning a result. This is done by executing a return statement. The syntax for this is: return <expression>; where the type of <expression> must match the type giving in the method heading. When a return statement is executed, control immediately goes back to the point of the method call with the result replacing the call. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 8 Example: Cube Method Here is a method that computes and returns the cube of its number parameter. modifier return type name static double Cube (int n) { double nCubed; nCubed = n * n * n; return nCubed; } parameter list return statement Type this into the HelloWorld program. It goes above the Main method, but inside the class as shown on the next slide. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 9 Example: Cube Method inside the class Cube method definition above Main( ) Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 10 Example: Cube Method Since this Cube( ) method is defined inside the same class as the Main( ) method, it is called without any prefix. For example, to ask the user for a number and then compute and display the cube of the number, add the following to Main ( ): Console.Write ("Enter a number: "); userInput = Console.ReadLine (); double number = double.Parse (userInput); double numberCubed = Cube (number); // method call Console.WriteLine ("The cube of {0:F} is {1:F}", number, numberCubed); Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 11 Example: Cube Method The flow of control for the method call to Cube is shown below: method call matches up argument to parameter and goes to start of the method body Monday, January 24 method return goes back to point of the call with the result replacing the call CS 205 Programming for the Sciences - Lecture 6 12 In-Class Exercise Modify the Circumference program by writing two new methods, ComputeCircumference and ComputeArea, that receive a radius number and return the circumference and area, respectively, of a circle with that radius. Both functions should used the pre-defined constant Math.PI Then modify the Main program so that it calls the new methods to compute the circumference and area instead of computing these values directly in Main. Monday, January 24 CS 205 Programming for the Sciences - Lecture 6 13