Introduction to the Parameters of a Function
Introduction to the Parameters of a Function
Introduction to External Values on Functions
Using Fields
Sometimes, to perform its operation, a function may need values not available in its body. Such values must be provied one way or another outside the function. One way to solve this problem is to declare one or more variables outside the function, and then use such variable(s) in the function.
Practical Learning: Introducing Functions
Control | (Name) | Text |
Label | Side: | |
TextBox | txtSideInCentimeters | 0.00 |
Label | Centimeters | |
Button | btnConvert | Convert |
Label | Perimeter: | |
TextBox | txtSideInInches | |
Label | Inches |
Introduction to Functions with One Parameter
A function may need to be given a value in other to perform its job. Such a value must be created outside the function. A value that is given to a function is called a paramater. Like a variable, a parameter is represented by a type of value and a name. Therefore, when creating a function that uses a parameter, in the parentheses of the function, enter the data type and the name of the parameter. Here is an example:
void Display(string s)
{
}
In the body of the function, you can ignore or use the parameter. The parameter is used by its name. Here is an example:
void Display(string s) { string message = s; }
A function that takes a parameter can also return a value. The return type is independent of the parameter. The return type and the parameter can be of the same or different types.
Practical Learning: Introducing Function and their Parameters
using System;
using System.Windows.Forms;
namespace ValuesConversions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double ConvertToInches()
{
double result;
double side = double.Parse(txtSideInCentimeters.Text);
result = side * 0.3937;
return result;
}
void ShowSideInInches(double number)
{
txtSideInInches.Text = number.ToString();
}
}
}
using System;
using System.Windows.Forms;
namespace ValuesConversions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double ConvertToInches()
{
double result;
double side = double.Parse(txtSideInCentimeters.Text);
result = side * 0.3937;
return result;
}
void ShowSideInInches(double number)
{
txtSideInInches.Text = number.ToString();
}
private void BtnConvert_Click(object sender, EventArgs e)
{
double conversion = 0.00;
}
}
}
Calling a Function of One Parameter
To call a function that uses a parameter, you must provide a value for that parameter. The value is supplied in the parentheses of the function that is being called. The value supplied to the function is called an argument. The action of providing that value is called passing an argument to the function.
To pass an argument to a function, you have many options. If you already know the value you want to use, enter it in the parentheses of the function. Here is an example:
void Display(string s)
{
}
void Evaluate()
{
Display("Welcome to C# Programming!");
}
As an alternative, you can first declare a variable and initialize it with the desired value. You can then pass the name of that variable as argument. The names of the parameters and argument don't have to the same; they can be different. Here is an example:
void Display(string s) { } void Evaluate() { string strMessage = "Welcome to C# Programming!"; // Blah blah blah Display(strMessage); }
Sometimes the value of the argument is created by other means or it is returned by another function. You can first declare a variable to hold that value and pass that variable as argument. Here is an example:
void Display(string s) { } string Create() { return "This is an amazing world."; } void Evaluate() { string msg = Create(); // Blah blah blah Display(msg); }
Remember that you usually declare a variable if you are planning to use a certain value many times. If not, you may not need such a variable. In the same way, if the variable is used to get the returned value of a function but you are planning to call that function only once, you can call that function directly where you will need it, including in the parentheses of a function that needs that value. Here is an example:
void Display(string s)
{
}
string Create()
{
return "This is an amazing world.";
}
void Evaluate()
{
Display(Create());
}
Practical Learning: Calling a Function of One Parameter
using System;
using System.Windows.Forms;
namespace ValuesConversions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double ConvertToInches()
{
double result;
double side = double.Parse(txtSideInCentimeters.Text);
result = side * 0.3937;
return result;
}
void ShowSideInInches(double number)
{
txtSideInInches.Text = number.ToString();
}
private void BtnConvert_Click(object sender, EventArgs e)
{
double conversion = ConvertToInches();
ShowSideInInches(conversion);
}
}
}
using System; using System.Windows.Forms; namespace ValuesConversions { public partial class Form1 : Form { public Form1() { InitializeComponent(); } double ConvertToInches() { double result = double.Parse(txtSideInCentimeters.Text) * 0.3937; return result; } void ShowSideInInches(double number) { txtSideInInches.Text = number.ToString(); } private void BtnConvert_Click(object sender, EventArgs e) { ShowSideInInches(ConvertToInches()); } } }
using System;
using System.Windows.Forms;
namespace ValuesConversions
{
public partial class MetricSystem : Form
{
public MetricSystem()
{
InitializeComponent();
}
private double ConvertToInches() => double.Parse(txtSideInCentimeters.Text) * 0.3937;
private void ShowSideInInches(double number)
{
txtSideInInches.Text = number.ToString();
}
private void BtnConvert_Click(object sender, EventArgs e)
{
ShowSideInInches(ConvertToInches());
}
}
}
Control | (Name) | Text |
Label | Purchase Price: | |
TextBox | txtPurchasePrice | 0.00 |
Label | Tax Rate: | |
TextBox | txtTaxRate | 0.00 |
Label | % | |
Button | btnCalculate | Calculate |
Label | Tax Amount: | |
TextBox | txtTaxAmount |
A Function With Many Parameters
Introduction
A function can use as many parameters as you want. Each parameter must have a data type and a name. The parameters can be of the same or different types. The parameters are separated by comas. Here is an example:
class Something
{
private void ShowResult(string s, double n)
{
}
}
Practical Learning: Creating a Function With Many Parameters
using System.Windows.Forms;
namespace BusinessMathematics1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double CalculateTaxAmount(double price, double rate)
{
return price * rate / 100;
}
}
}
using System.Windows.Forms;
namespace BusinessMathematics1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double CalculateTaxAmount(double price, double rate)
{
return price * rate / 100;
}
private void btnCalculate_Click(object sender, System.EventArgs e)
{
double price = double.Parse(txtPurchasePrice.Text);
double rate = double.Parse(txtTaxRate.Text);
double amount = 0.00;
txtTaxAmount.Text = amount.ToString("F");
}
}
}
Introduction to Calling a Function of Many Parameters
We saw that, when calling a function that takes an argument, you can type a value in the parentheses of the function or you can pass the name of a variable that holds the value to pass. In the same way, when calling a function that takes more than one argument, type the appropriate value in the placeholder of each argument.
Practical Learning: Calling a Function of Many Parameters
using System.Windows.Forms;
namespace BusinessMathematics1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double CalculateTaxAmount(double price, double rate)
{
return price * rate / 100;
}
private void btnCalculate_Click(object sender, System.EventArgs e)
{
double price = double.Parse(txtPurchasePrice.Text);
double rate = double.Parse(txtTaxRate.Text);
double amount = CalculateTaxAmount(price, rate);
txtTaxAmount.Text = amount.ToString("F");
}
}
}
Control | (Name) | Text |
Label | Contractor Code: | |
TextBox | txtContractorCode | |
Label | Hourly Salary: | |
TextBox | txtHourlySalary | 0.00 |
Label | First Name | |
TextBox | txtFirstName | |
Label | Last Name | |
TextBox | txtLastName | |
Label | Time Worked - Week 1: | |
TextBox | txtWeek1TimeWorked | 0.00 |
Label | Week 2: | |
TextBox | txtWeek2TimeWorked | 0.00 |
Button | btnShowPayroll | Present Payroll |
Label | Contractor Code: | |
TextBox | txtEmployeeNumber | |
Label | Net Pay: | |
TextBox | txtNetPay | |
Label | Employee Name: | |
TextBox | txtEmployeeName |
uusing System.Windows.Forms; namespace PayrollEvaluation1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string GetContractorName(string fname, string lname) { return fname + " " + lname; } double CalculateSalary(double salary, double week1Work, double week2Work) { double totalTimeWorked = week1Work + week2Work; double biweeklyTotal = salary * totalTimeWorked; return biweeklyTotal; } private void BtnShowPayroll_Click(object sender, EventArgs e) { double week1 = double.Parse(txtWeek1TimeWorked.Text); double week2 = double.Parse(txtWeek2TimeWorked.Text); } } }
using System; using System.Windows.Forms; namespace PayrollEvaluation1 { public partial class Form1 : Form { public PayrollEvaluation() { InitializeComponent(); } string GetContractorName(string fname, string lname) { return fname + " " + lname; } double CalculateSalary(double salary, double week1Work, double week2Work) { double totalTimeWorked = week1Work + week2Work; double biweeklyTotal = salary * totalTimeWorked; return biweeklyTotal; } private void btnShowPayroll_Click(object sender, EventArgs e) { string workCode = txtContractorCode.Text; string firstName = txtFirstName.Text; string lastName = txtLastName.Text; double hSalary = double.Parse(txtHourlySalary.Text); double week1 = double.Parse(txtWeek1TimeWorked.Text); double week2 = double.Parse(txtWeek2TimeWorked.Text); double salary = CalculateSalary(hSalary, week1, week2); txtEmployeeNumber.Text = workCode; txtEmployeeName.Text = GetContractorName(firstName, lastName); txtNetPay.Text = salary.ToString("F"); } } }
Accessing a Parameter by Name
If you call a function that takes many arguments, you don't have to use the exact placeholder of each parameter. Instead, you can refer to each argument by the name of its parameter, followed by a colon, and followed by the appropriate value.
Practical Learning: Accessing a Parameter by Name
using System; using System.Windows.Forms; namespace PayrollEvaluation1 { public partial class Form1 : Form { public PayrollEvaluation() { InitializeComponent(); } string GetContractorName(string fname, string lname) { return fname + " " + lname; } double CalculateSalary(double salary, double week1Work, double week2Work) { double totalTimeWorked = week1Work + week2Work; return salary * totalTimeWorked; } private void btnShowPayroll_Click(object sender, EventArgs e) { string workCode = txtContractorCode.Text; string firstName = txtFirstName.Text; string lastName = txtLastName.Text; double hSalary = double.Parse(txtHourlySalary.Text); double week1 = double.Parse(txtWeek1TimeWorked.Text); double week2 = double.Parse(txtWeek2TimeWorked.Text); double salary = CalculateSalary(week2Work: week2, week1Work: week1, salary: hSalary); txtEmployeeNumber.Text = workCode; txtEmployeeName.Text = GetContractorName(lname: lastName, fname : firstName); txtNetPay.Text = salary.ToString("F"); } } }
using System; using System.Windows.Forms; namespace PayrollEvaluation1 { public partial class Form1 : Form { public PayrollEvaluation() { InitializeComponent(); } string GetContractorName(string fname, string lname) => fname + " " + lname; double CalculateSalary(double salary, double week1Work, double week2Work) => salary * (week1Work + week2Work); private void btnShowPayroll_Click(object sender, EventArgs e) { string workCode = txtContractorCode.Text; string firstName = txtFirstName.Text; string lastName = txtLastName.Text; double hSalary = double.Parse(txtHourlySalary.Text); double week1 = double.Parse(txtWeek1TimeWorked.Text); double week2 = double.Parse(txtWeek2TimeWorked.Text); double salary = CalculateSalary(week2Work: week2, week1Work: week1, salary: hSalary); txtEmployeeNumber.Text = workCode; txtEmployeeName.Text = GetContractorName(lname: lastName, fname : firstName); txtNetPay.Text = salary.ToString("F"); } } }
|
||
Previous | Copyright © 2001-2021, C# Key | Next |
|