You probably have used mathematical functions in algebra class, but they all had calculated values associated with them. For instance if you defined
f(x)=x2
then it follows that f(3) is 32, and f(3)+f(4) is 32 + 42
Function calls in expressions get replaced during evaluation by the value of the function.
The corresponding definition and examples in C# would be the following, taken from example program return1.cs. Read and run:
using System;
class Return1
{
static int f(int x)
{
return x*x;
}
static void Main()
{
Console.WriteLine(f(3));
Console.WriteLine(f(3) + f(4));
}
}
The new C# syntax is the return statement, with the word return followed by an expression. Functions that return values can be used in expressions, just like in math class. When an expression with a function call is evaluated, the function call is effectively replaced temporarily by its returned value. Inside the C# function, the value to be returned is given by the expression in the return statement.
Since the function returns data, and all data in C# is typed, there must be a type given for the value returned. Note that the function heading does not start with static void. In place of void is int. The void in earlier function headings meant nothing was returned. The int here means that a value is returned and its type is int.
After the function f finishes executing from inside
Console.WriteLine(f(3));
it is as if the statement temporarily became
Console.WriteLine(9);
and similarly when executing
Console.WriteLine(f(3) + f(4));
the interpreter first evaluates f(3) and effectively replaces the call by the returned result, 9, as if the statement temporarily became
Console.WriteLine(9 + f(4));
and then the interpreter evaluates f(4) and effectively replaces the call by the returned result, 16, as if the statement temporarily became
Console.WriteLine(9 + 16);
resulting finally in 25 being calculated and printed.
C# functions can return any type of data, not just numbers, and there can be any number of statements executed before the return statement. Read, follow, and run the example program return2.cs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System;
class Return2
{
static string lastFirst(string firstName, string lastName)
{
string separator = ", ";
string result = lastName + separator + firstName;
return result;
}
static void Main()
{
Console.WriteLine(lastFirst("Benjamin", "Franklin"));
Console.WriteLine(lastFirst("Andrew", "Harrington"));
}
}
|
Many have a hard time following the flow of execution with functions. Even more is involved when there are return values. Make sure you completely follow the details of the execution:
Compare return2.cs and addition1.cs, from the previous section. Both use functions. Both print, but where the printing is done differs. The function sumProblem prints directly inside the function and returns nothing explicitly. On the other hand lastFirst does not print anything but returns a string. The caller gets to decide what to do with the string, and above it is printed in Main.
In general functions should do a single thing. You can easily combine a sequence of functions, and you have more flexibility in the combinations if each does just one unified thing. The function sumProblem in addition1.cs does two thing: It creates a sentence, and prints it. If that is all you have, you are out of luck if you want to do something different with the sentence string. A better way is to have a function that just creates the sentence, and returns it for whatever further use you want. After returning that value, printing is one possibility, done in addition2.cs:
using System;
class Addition2
{
static string sumProblemString(int x, int y)
{
int sum = x + y;
string sentence = "The sum of " + x + " and " + y + " is " + sum + ".";
return sentence;
}
static void Main()
{
Console.WriteLine(sumProblemString(2, 3));
Console.WriteLine(sumProblemString(12345, 53579));
Console.Write("Enter an integer: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter another integer: ");
int b = int.Parse(Console.ReadLine());
Console.WriteLine(sumProblemString(a, b));
}
}
In class recommendation: Improve Miles’ original example with functions. What makes sense? The original example is saved as GlazerCalc1.cs.
Create quotientReturn.cs by modifying quotientProb.cs in Quotient Function Exercise so that the program accomplishes the same thing, but everywhere change the quotientProblem function into one called quotientString that merely returns the string rather than printing the string directly. Have Main print the result of each call to the quotientString function.