You are on page 1of 23

Windows Programming

October 2016
Functions
Topics
• Defining and Using Functions
• Return Values
• Parameters
– Parameter Array s
– Reference and Value Parameters
• Struct Functions
• Function Overloading

01/01/2021 Windows Programming 3


Functions

• means of providing blocks of code that can be


executed at any point in an application
• Why functions?
– redundant tasks
• Code length
• Maintenance
– code reusability
– multipurpose code

01/01/2021 Windows Programming 4


Functions
• may return a value
• may need information (parameters)
• may be:
– instance
– static
• Signature is name + parameter
• Can have functions in a class with same name
but different signatures - overloading
01/01/2021 Windows Programming 5
DEFINING AND USING FUNCTIONS
class Program
{
static void Write()
{
Console.WriteLine("Text output from function.");
}
static void Main(string[] args)
{
Write();
Console.ReadKey();
}
}
01/01/2021 Windows Programming 6
Return Values

• way to exchange data with a function


– double vat;
– vat = getVat();
• things to consider
– type must be specified
– return keyword

01/01/2021 Windows Programming 7


Return Values
static <returnType> <FunctionName>()
{
...
return <returnValue>;
}
• When return statement is reached, program
execution returns to the calling code
• return can be used in void functions

01/01/2021 Windows Programming 8


Return Values
static double GetVal() static double GetVal()
{ {
double checkVal; double checkVal;
// CheckVal assigned a // CheckVal assigned a
//value through some value through some
//logic (not shown here). logic.
if (checkVal < 5) if (checkVal < 5)
return 4.7; return 4.7;
return 3.2; }
}
01/01/2021 Windows Programming 9
Parameters
• must specify
– parameter list with type
– matching list during call

static <returnType> <FunctionName>(<paramType>


<paramName>, ...)
{
...
return <returnValue>;
}
01/01/2021 Windows Programming 10
Parameters: Example
static double Product(double param1, double param2)
{
return param1 * param2;
}

static int MaxValue(int[] intArray)


{
int maxVal = intArray[0];
for (int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}
01/01/2021 Windows Programming 11
Parameters
• calling a function requires parameter matching
static void MyFunction(string myString, double myDouble)
{
...
}
• the appropriate way of calling?
– MyFunction(“String”, 12);
– MyFunction(12, “String”);
– MyFunction(12);

01/01/2021 Windows Programming 12


Parameter Arrays
• last parameter in a function
• allows to call functions using a variable amount of
parameters
• params keyword
Definition
static <returnType> <FunctionName>(<p1Type>
<p1Name>, ..., params <type>[] <name>)
{
...
return <returnValue>;
}
call: <FunctionName>(<p1>,
01/01/2021
..., <val1>, <val2>, ...)
Windows Programming 13
Parameter Arrays

• useful for specifying additional information for


functions

01/01/2021 Windows Programming 14


Reference and Value Parameters
• C# offers three options:
– pass-by-value (default)
– pass-by-reference
– pass-by-result ("copy-out")

01/01/2021 Windows Programming 15


Reference and Value Parameters
• Pass by value is default parameter passing
mechanism
– data copied into function
– any changes to parameter inside method affect
local copy only
value parameter void F(int x)
{
x = 0;
}

int y = 9;

y unchanged F(y);

01/01/2021 Windows Programming 16


Reference and Value Parameters
• by reference: ref parameter passes data in & out
– use keyword ref in definition and call
– must use variable in call
– must initialize passed variable before call
ref parameter,
void G(ref int x)
initially 9
{
x += 1;
}

int y = 9;

y set to 10 G(ref y);

01/01/2021 Windows Programming 17


Reference and Value Parameters
• limitations on variable used as ref parameter
• must be non-constant value
• must use initialized variable

01/01/2021 Windows Programming 18


Reference and Value Parameters
• by result: out parameter returns data through
parameter
– use keyword out in both definition and call
– must use variable in call
– must assign to parameter inside method or compiler
error
out parameter void H(out int x)
{
assignment required x = 0;
}

int y;

y set to 0 H(out y);


01/01/2021 Windows Programming 19
Reading Assignment
• Variable scope
• Main function in detail

01/01/2021 Windows Programming 20


STRUCT FUNCTIONS
• structs may contain functions as well as data

struct CustomerName
{
public string firstName, lastName;
public string Name()
{
return firstName + " " + lastName;
}
};
01/01/2021 Windows Programming 21
OVERLOADING FUNCTIONS
• to create multiple functions with same name working with
different parameter types

static double MaxValue(double[] doubleArray)


{
double maxVal = doubleArray[0];
for (int i = 1; i < doubleArray.Length; i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal;
}
01/01/2021 Windows Programming 22
OVERLOADING FUNCTIONS
• overloading is possible as long as signatures
differ
• you could have functions
• that take parameters by value and by reference
• that differ in the number of parameters they require
• no need to explicitly specify which function
you want to use

01/01/2021 Windows Programming 23

You might also like