0% found this document useful (0 votes)
16 views

Chapter 06+W10

Study notes for c#

Uploaded by

koketsomolete880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 06+W10

Study notes for c#

Uploaded by

koketsomolete880
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 6

6.1 – 6.3

Modularizing Your
Code with Methods
Topics
6.1 Introduction to Methods
6.2 void Methods
6.3 Passing Arguments to Methods
6.4 Passing Arguments by Reference
6.5 Value-Returning Methods
6.6 Debugging Methods
6.1 Introduction to Methods
• Methods can be used to break a complex program into
small, manageable pieces
– This approach is known as divide and conquer
– In general terms, breaking down a program to smaller units of
code, such as methods, is known as modularization
• Two types of methods are:
– A void method simply executes a group of statements and then
terminates
– A value-returning method returns a value to the statement that
called it
Example
Using one long sequence of statement to perform a task

Namespace Example
{
public partial class Form1 : Form
{
private void myButton_Click(object sender, EventArgs e)
{
statement;
statement;
statement;
statement;
statement;
statement;

}
}
}
Example
Using methods to divide and conquer a problem
Namespace Example
{
public partial class Form1 : Form
{
private void myButton_Click(object sender, EventArgs e)
{
Method2();
Method3();

}

private void Method2()


{
statements;
}

private void Method3()


{
statements;
}
}
}
6.2 void Methods
• A void method simply executes the statement it
contains and then terminates. It does not return
any value to the statement that called it
• To create a method you write its definitions
• A method definition has two parts:
– header: the method header appears at the beginning
of a method definition to indicate access mode, return
type, and method name
– body: the method body is a collection of statements
that are performed when the method is executed
The Method Header
• The book separates a method header into four parts :
– Access modifier: keywords that defines the access control
• private: a private method can be called only by code inside the same class as the
method
• public: a public method can be called by code that is outside the class.
– Return type: specifies whether or not a method returns a value
– Method name: the identifier of the method; must be unique in a given
program. This book uses Pascal case (aka camelCase)
– Parentheses: A method's name is always followed by a pair of
parentheses
Access Return Method Parentheses
modifier type name

private void DisplayMessage()


{
MessageBox.Show("This is the DisplayMessage method.");
}
Declaring Methods Inside a Class
• Methods usually belong to a class
• All Visual C# methods typically belong to applications' default Form1 class
• In this book, methods are created inside the Form1 class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

// your method definition will appear here inside Form1 class


}
}
Calling a Method
• A method executes when it is called
• Event handlers are called when specific events take place. Yet,
methods are executed by method call statements.
• A method call statement is the name of the method followed by a
pair of parentheses. For example:

private void goButton_Click(object sender, EventArgs e)


{
MessageBox.Show("This is the goButton_Click method.");
DisplayMessage();
}

private void DisplayMessage()


{
MessageBox.Show("This is the DisplayMessage method.");
}
Concept of Return Point
• When calling a method the system needs to know where
the program should return after the method ends
• The system saves the memory address of the location
called return point to which it should return
• The system jumps to the method and executes the
statements in its body
• When the method ends, the system jumps back to the
return point and resumes execution
Top-Down Design
• To modularize a program, programmers commonly use a
technique known as top-down design
• It breaks down an algorithm to methods
• The process is performed in the following manner:
– The overall task that the program is to perform is broken down
into a series of subtasks
– Each subtask is examined to determine whether it can be further
broken down into more subtasks. This step is repeated until no
more subtasks can be identified
– Once all the subtasks have been identified, they are written in
code
6.3 Passing Arguments to Methods
• An argument is any piece of data that is passed into a method when the
method is called
– In the following, the statement calls the MessageBox.Show method and passes
the string "Hello" as an argument:

MessageBox.Show("Hello");
• A parameter is a variable that receives an argument that is passed into a
method
– In the following, value is an int parameter:

private void DisplayValue(int value)


{
MessageBox.Show(value.ToString());
}

– An example of a call to DisplayValue method with 5 as parameter is:


DisplayValue(5);
Contents of Variables as Arguments
• You can pass the contents of variables as arguments. For example,
Take
Take
note
note
that
that
thethe
argument
argument(x)
and parameter
and parameter
(value)have
can have
different
differentnames
names

int x = 5; private void DisplayValue(int value)


{
DisplayValue(x);
MessageBox.Show(value.ToString());
DisplayValue(x * 4); }

• value is an int parameter in the DisplayValue method


• In this example, x is an int variable with the value 5. Its contents
are passed as argument.
• The expression x * 4 also produces an int result, which can be
passed as an argument.
• Another example is:
DisplayValue(int.Parse("700"));
Argument and Parameter Data Type
Compatibility
• An argument's data type must be assignment compatible
with the receiving parameter's data type
• Basically,
– You can pass only string arguments into string parameters
– You can pass int arguments into int parameters, but you
cannot pass double or decimal arguments into int parameters
– You can pass either double or int arguments to double
parameters, but you cannot pass decimal values to double
parameters
– You can pass either decimal or int arguments to decimal
parameters, but you cannot pass double arguments into
decimal parameters
Passing Multiple Arguments
• You can pass more than one argument to
a method
private void showButton1_Click(object sender, EventArgs e)
{
ShowMax(5, 10);
}

private void showButton2_Click(object sender, EventArgs e)


{
int value1 = 2;
int value2 = 3;
ShowMax(value1, value2);
}

private void ShowMax(int num1, int num2) { }


Named Arguments
• C# allows you to specify which parameter an argument should be
passed into. The syntax is:
parameterName : value
• An argument that is written using this syntax is known as a named
argument
private void showButton_Click(object sender, EventArgs e)
{
showName(lastName : "Smith", firstName : "Suzanne");
}
private void ShowName(string firstName, string lastName)
{
MessageBox.Show(firstName + " " + lastName);
}

• Notice that you get the same result if the call statement is:

showName("Suzanne", "Smith");
Default Arguments
• C# allows you to provide a default argument for a method
parameter
private void ShowTax(decimal price, decimal taxRate = 0.07m)
{
decimal tax = price * taxRate;
}

• The value of taxRate is defaulted to 0.07m. You can simply call the
method by passing only the price
ShowTax(100.0m);

• You can also override the default argument


ShowTax(100.0m, 0.08m);

You might also like