Introduction to Functions
Introduction to Functions
Functions Fundamentals
Introduction
As seen in our first introduction to methods, a function is a section of code that performs an action. You create a function by writing code. The fundamental formula to follow when creating a function is:
options function-name(...)
{
}
As you may know already, code in C# must be written inside an object layout, such as a class (or a structure). In C#, console and Windows Forms applications don't allow functions (created outside a class (or structures)), but other types of C# applications, such as web applications, allow functions. In our series of lessons here, a function is a section of code that behaves like it is created and used outside a class. In other words, we use functions here as indipendent entities, that is independent from a class.
Practical Learning: Introducing Functions
Control | (Name) | Text |
Label | Hourly Salary: | |
TextBox | txtHourlySalary | 0.00 |
Button | btnCalculate | Calculate |
Label | _______________________ | |
Label | Daily Salary: | |
TextBox | txtDailySalary | |
Label | Weekly Salary: | |
TextBox | txtWeeklySalary | |
Label | Biweekly Salary: | |
TextBox | txtBiweeklySalary | |
Label | Monthly Salary: | |
TextBox | txtMonthlySalary | |
Label | Yearly Salary: | |
TextBox | txtYearlySalary |
A Void Function
The creation of a function starts with some options. Later, we will learn more about them in next sections and lessons. For a simple function, specify the option with the void keyword.
The Name of a Function
A function must have a name. That name must follow some rules:
The Parentheses of a Function
The name of a function is followed by parentheses. At a minimum, the parentheses can be empty. Most of the time, you will writteen the parentheses on the same line as the name of the function. Otherwise, you can write each parenthesis on its own line. Here is an example:
void HelpMe ( )
Or like this:
void HelpMe( )
Or like this:
void HelpMe ( )
The Body of a Function
The body of a function follows the parentheses. After the parentheses, add some brackets { and }. The section between the curly brackets is the body of the function. In that body, you can write the code that describes what the function is supposed to do.
Practical Learning: Creating a Fuction
using System.Windows.Forms;
namespace SalaryEvaluation1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void PresentValues()
{
double hourlySalary = double.Parse(txtHourlySalary.Text);
double dailySalary = hourlySalary * 8;
double weeklySalary = hourlySalary * 40;
double biweeklySalary = weeklySalary * 2;
double monthlySalary = weeklySalary * 4;
double yearlySalary = monthlySalary * 12;
txtDailySalary.Text = dailySalary.ToString();
txtWeeklySalary.Text = weeklySalary.ToString();
txtBiweeklySalary.Text = biweeklySalary.ToString();
txtMonthlySalary.Text = monthlySalary.ToString();
txtYearlySalary.Text = yearlySalary.ToString();
}
}
}
Calling a Function
After creating a function, you can use it. Using a function is referred to as calling it. To call a simple like the one we created above, type its name, followed by parentheses, and it with a semicolon.
Practical Learning: Calling a Function
using System.Windows.Forms;
namespace SalaryEvaluation1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void PresentValues
(
)
{
double hourlySalary = double.Parse(txtHourlySalary.Text);
double dailySalary = hourlySalary * 8;
double weeklySalary = hourlySalary * 40;
double biweeklySalary = weeklySalary * 2;
double monthlySalary = weeklySalary * 4;
double yearlySalary = monthlySalary * 12;
txtDailySalary.Text = dailySalary.ToString();
txtWeeklySalary.Text = weeklySalary.ToString();
txtBiweeklySalary.Text = biweeklySalary.ToString();
txtMonthlySalary.Text = monthlySalary.ToString();
txtYearlySalary.Text = yearlySalary.ToString();
}
private void btnCalculate_Click(object sender, System.EventArgs e)
{
PresentValues();
}
}
}
Control | (Name) | Text |
Label | Meter Reading Start . . . . . . . . . . . . | |
TextBox | txtMeterReadingStarStart | 0 |
Label | Gallons | |
Label | Meter Reading End . . . . . . . . . . . . | |
TextBox | txtMeterReadingStarEnd | 0 |
Label | Gallons | |
Button | btnCalculateWaterAndSewerUsage | Calculate Water and Sewer Usage |
Label | _______________________________ | |
Label | Water and Sewer Usage . . . . . . . . . . . . | |
TextBox | txtWaterSewerUsage | 0 |
Label | Gallons | |
Label | Water Use Charges => 4.18 per 1,000 Gallons . . . . . . . . . . . . . . . . . . . . . . | |
TextBox | txtSewerUseCharges | 0.00 |
Label | Sewer Use Charges => 5.85 per 1,000 Gallons . . . . . . . . . . . . . . . . . . . . . | |
TextBox | txtSewerUseCharges | 0.00 |
using System.Windows.Forms; namespace WaterCompany5 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } void CalculteWaterSewer() { double usage; double meterReadingStart = int.Parse(txtMeterReadingStart.Text); double meterReadingEnd = int.Parse(txtMeterReadingEnd.Text); usage = meterReadingEnd - meterReadingStart; double waterConsumption = (usage * 4.18) / 1000; double sewerServices = (usage * 5.85) / 1000; txtWaterSewerUsage.Text = usage.ToString(); txtWaterUseCharges.Text = waterConsumption.ToString("F"); txtSewerUseCharges.Text = sewerServices.ToString("F"); } private void btnCalculateWaterAndSewerUsage_Click(object sender, System.EventArgs e) { CalculteWaterSewer(); } } }
Control | (Name) | Text |
Label | Meter Reading Start . . . . . . . . . . . . | |
TextBox | txtMeterReadingStarStart | 0 |
Label | Gallons | |
Label | Meter Reading End . . . . . . . . . . . . | |
TextBox | txtMeterReadingStarEnd | 0 |
Label | Gallons | |
Button | btnCalculateWaterAndSewerUsage | Calculate Water and Sewer Usage |
Label | _______________________________ | |
Label | Water and Sewer Usage . . . . . . . . . . . . | |
TextBox | txtWaterSewerUsage | 0 |
Label | Gallons | |
Label | Water Use Charges => 4.18 per 1,000 Gallons . . . . . . . . . . . . . . . . . . . . . . | |
TextBox | txtSewerUseCharges | |
Label | Sewer Use Charges => 5.85 per 1,000 Gallons . . . . . . . . . . . . . . . . . . . . . | |
TextBox | txtSewerUseCharges | |
Label | Distribution Charges . . . . . . . . . . . . . . | |
TextBox | txtDistributionCharges | 0.00 |
Label | Environment Charges . . . . . . . . . . . . . . | |
TextBox | txtEnvironmentCharges | 0.00 |
Button | btnCalculateTotalCharges | Calculate Total Charges |
Label | _______________________________ | |
Label | Total Charges . . . . . . . . . . . . | |
TextBox | txtTotalCharges |
A Function that Returns a Value
A Function that Returns Nothing
Practially every function has a body, which is delimited by curly brackets. Still, to indicate the end of execution of a function, type the return keyword followed by a semicolon. Here are examples:
class Exercise { void Communicate() { return; } void NeedToDiscuss() { return; } static void Main() { return; } }
Introduction to Returning a Value
Sometimes, you will want a function that gives back a value from its operation(s). To start, when creating the function, on the left side of the name of the function, instead of void, write the data type of the value that the function will produce. Here is an example:
int Calculate() { }
In the body of the function, perform the necessary operation. Before the closing curly bracket, type the return keyword, followed by the value the function must return, and end the statement with a semicolon.
Practical Learning: Returning a Value from a Function
using System.Windows.Forms;
namespace WaterCompany5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void CalculteWaterSewer(
)
{
double usage;
double meterReadingStart = int.Parse(txtMeterReadingStart.Text);
double meterReadingEnd = int.Parse(txtMeterReadingEnd.Text);
usage = meterReadingEnd - meterReadingStart;
double waterConsumption = (usage * 4.18) / 1000;
double sewerServices = (usage * 5.85) / 1000;
txtWaterSewerUsage.Text = usage.ToString();
txtWaterUseCharges.Text = waterConsumption.ToString("F");
txtSewerUseCharges.Text = sewerServices.ToString("F");
}
private void btnCalculateWaterAndSewerUsage_Click(object sender, System.EventArgs e)
{
CalculteWaterSewer();
}
double CalculateSewerUsage()
{
double amountDue;
double waterUseCharges;
double sewerUseCharges;
double distributionCharges;
double environmentCharges;
waterUseCharges = double.Parse(txtWaterUseCharges.Text);
sewerUseCharges = double.Parse(txtSewerUseCharges.Text);
distributionCharges = double.Parse(txtDistributionCharges.Text);
environmentCharges = double.Parse(txtEnvironmentCharges.Text);
amountDue = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges;
return amountDue;
}
}
}
Calling a Function that Returns a Value
You can call a function that returns a value as you do for one that is void. If you want to get and use the value returned by the function, you can declare a variable of the return type and assign the call to that variable. The variable would then be carrying the return value of the function and you can use that value any way you want.
Practical Learning: Calling a Function that Returns a Value
using System.Windows.Forms;
namespace WaterCompany5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void CalculteWaterSewer
(
)
{
double usage;
double meterReadingStart = int.Parse(txtMeterReadingStart.Text);
double meterReadingEnd = int.Parse(txtMeterReadingEnd.Text);
usage = meterReadingEnd - meterReadingStart;
double waterConsumption = (usage * 4.18) / 1000;
double sewerServices = (usage * 5.85) / 1000;
txtWaterSewerUsage.Text = usage.ToString();
txtWaterUseCharges.Text = waterConsumption.ToString("F");
txtSewerUseCharges.Text = sewerServices.ToString("F");
}
private void btnCalculateWaterAndSewerUsage_Click(object sender, System.EventArgs e)
{
CalculteWaterSewer();
}
double CalculateSewerUsage()
{
double amountDue;
double waterUseCharges;
double sewerUseCharges;
double distributionCharges;
double environmentCharges;
waterUseCharges = double.Parse(txtWaterUseCharges.Text);
sewerUseCharges = double.Parse(txtSewerUseCharges.Text);
distributionCharges = double.Parse(txtDistributionCharges.Text);
environmentCharges = double.Parse(txtEnvironmentCharges.Text);
amountDue = waterUseCharges + sewerUseCharges + distributionCharges + environmentCharges;
return amountDue;
}
private void btnCalculateTotalCharges_Click(object sender, System.EventArgs e)
{
double invoice = CalculateSewerUsage();
txtTotalCharges.Text = invoice.ToString();
}
}
}
using System.Windows.Forms;
namespace WaterCompany5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void CalculteWaterSewer()
{
. . .
}
private void btnCalculateWaterAndSewerUsage_Click(object sender, System.EventArgs e)
{
CalculteWaterSewer();
// The following line is optional
return;
}
double CalculateSewerUsage()
{
. . .
return amountDue;
}
private void btnCalculateTotalCharges_Click(object sender, System.EventArgs e)
{
txtTotalCharges.Text = CalculateSewerUsage().ToString();
// The following line is optional
return;
}
}
}
Control | (Name) | Text |
Label | Hourly Salary: | |
TextBox | txtHourlySalary | 0.00 |
Button | btnCalculate | Calculate |
Label | _______________________ | |
Label | Daily Salary: | |
TextBox | txtDailySalary | |
Label | Weekly Salary: | |
TextBox | txtWeeklySalary | |
Label | Biweekly Salary: | |
TextBox | txtBiweeklySalary | |
Label | Monthly Salary: | |
TextBox | txtMonthlySalary | |
Label | Yearly Salary: | |
TextBox | txtYearlySalary |
using System; using System.Windows.Forms; namespace SalaryEvaluation2 { public partial class Form1 : Form { double hourlySalary; public Form1() { InitializeComponent(); } double EvaluateDailyWages() { return hourlySalary * 8; } double EvaluateWeeklyWages() { return hourlySalary * 40; } double EvaluateBiweeklyWages() { return EvaluateWeeklyWages() * 2; } double EvaluateMonthlyWages() { return EvaluateWeeklyWages() * 4; } double EvaluateYearlyWages() { return EvaluateMonthlyWages() * 12; } private void btnCalculate_Click(object sender, EventArgs e) { hourlySalary = double.Parse(txtHourlySalary.Text); double dailySalary = EvaluateDailyWages(); double weeklySalary = EvaluateWeeklyWages(); double biweeklySalary = EvaluateBiweeklyWages(); double monthlySalary = EvaluateMonthlyWages(); double yearlySalary = EvaluateYearlyWages(); txtDailySalary.Text = dailySalary.ToString(); txtWeeklySalary.Text = weeklySalary.ToString(); txtBiweeklySalary.Text = biweeklySalary.ToString(); txtMonthlySalary.Text = monthlySalary.ToString(); txtYearlySalary.Text = yearlySalary.ToString(); } } }
using System;
using System.Windows.Forms;
namespace SalaryEvaluation2
{
public partial class Form1 : Form
{
double hourlySalary;
public Form1()
{
InitializeComponent();
}
. . .
private void btnCalculate_Click(object sender, EventArgs e)
{
hourlySalary = double.Parse(txtHourlySalary.Text);
txtDailySalary.Text = EvaluateDailyWages().ToString();
txtWeeklySalary.Text = EvaluateWeeklyWages().ToString();
txtBiweeklySalary.Text = EvaluateBiweeklyWages().ToString();
txtMonthlySalary.Text = EvaluateMonthlyWages().ToString();
txtYearlySalary.Text = EvaluateYearlyWages().ToString();
// The following line is optional
return;
}
}
}
A Simple Returning Method With no Body
If you have a function that either contains only one expression or the contents of its body can be resumed to one line, such a function doesn't need a body. Instead, after the parentheses of the function, type => followed by the returning expression.
Practical Learning: Creating a Method without a Body
using System; using System.Windows.Forms; namespace SalaryEvaluation2 { public partial class Form1 : Form { double hourlySalary; public Form1() { InitializeComponent(); } double EvaluateDailyWages() => hourlySalary * 8; double EvaluateWeeklyWages() => hourlySalary * 40; double EvaluateBiweeklyWages() { return EvaluateWeeklyWages() * 2; } double EvaluateMonthlyWages() { return EvaluateWeeklyWages() * 4; } double EvaluateYearlyWages() { return EvaluateMonthlyWages() * 12; } private void btnCalculate_Click(object sender, EventArgs e) { hourlySalary = double.Parse(txtHourlySalary.Text); txtDailySalary.Text = EvaluateDailyWages().ToString(); txtWeeklySalary.Text = EvaluateWeeklyWages().ToString(); txtBiweeklySalary.Text = EvaluateBiweeklyWages().ToString(); txtMonthlySalary.Text = EvaluateMonthlyWages().ToString(); txtYearlySalary.Text = EvaluateYearlyWages().ToString(); } } }
using System; using System.Windows.Forms; namespace SalaryEvaluation2 { public partial class Form1 : Form { double hourlySalary; public Form1() { InitializeComponent(); } double EvaluateDailyWages() => hourlySalary * 8; double EvaluateWeeklyWages() => hourlySalary * 40; double EvaluateBiweeklyWages() => EvaluateWeeklyWages() * 2; double EvaluateMonthlyWages() => EvaluateWeeklyWages() * 4; double EvaluateYearlyWages() => EvaluateMonthlyWages() * 12; private void btnCalculate_Click(object sender, EventArgs e) { hourlySalary = double.Parse(txtHourlySalary.Text); txtDailySalary.Text = EvaluateDailyWages().ToString(); txtWeeklySalary.Text = EvaluateWeeklyWages().ToString(); txtBiweeklySalary.Text = EvaluateBiweeklyWages().ToString(); txtMonthlySalary.Text = EvaluateMonthlyWages().ToString(); txtYearlySalary.Text = EvaluateYearlyWages().ToString(); } } }
|
||
Previous | Copyright © 2001-2021, C# Key | Next |
|