-
Notifications
You must be signed in to change notification settings - Fork 0
Unit 1 ‐ Lesson 2
In the previous lesson, we learned how to use the Console.WriteLine and Console.ReadLine functions. In this lesson, we will learn how to create our own functions (also called "methods").
Recall that a function has an input and an output. In programming terms, the input is called the arguments or the parameters. The output is called the return value; a function returns an output.
Let's make a function f(x) which performs the square operation. It will take in a number as its input and return the square of that number as its output.
Believe it or not, we already have a function defined in our ConsoleCalculator program already. That function is called Main
. The line static void Main(string[] args)
defines this function; Main
takes in string[] args
as its parameters ("args" is short for "arguments"). The curly brackets {
and }
define the beginning and end of the function, respectively.
After the }
which defines the end of the Main
method, press enter and add the line static void f(x)
. There is some red underlining which indicates that the compiler is not happy with the code that we just wrote. First we must define the type of our input variable x
. Change the x
to double x
. Again since the purpose of this method is to square a number, we must take a number as input; a string
, char
, etc. are not acceptable types for the operation we want to perform. A decimal number is an acceptable input, so we choose double
instead of int
, although we could use int
if we wanted to only take in integers and not decimal numbers.
Now we must define the beginning and end of our method. Press enter and add a {
character. Visual Studio will probably autocomplete your code and add a }
for you. Visual Studio should now consider this a valid function; the red underlining should go away. Press enter so that the end bracket is on a different line than the start.
Let's look at two words that don't make sense yet: static
and void
. We will not discuss what static
means for a few lessons. However, the void
keyword describes the return type of this function. void
means that there is no return type; the method does not produce output. Given what we know about the purpose of functions so far, this may not make sense. However, what does make sense is that we know that we do want to return something; specifically, the square of our input. And the square is a number, potentially a decimal. Therefore the return type of the function should be double
. Therefore, replace the void
with double
.
We have now defined a new function, though it doesn't do anything yet. Visual Studio is also unhappy with us again; we have defined the return type to be double but it correctly and helpfully is telling us (if you mouse over the red-underlined "f") "not all code paths return a value". This means that it is expecting a double
to be returned by the function but the code inside the function isn't actually returning anything; the function is not going to behave as expected.
Let's define what our function actually does and make sure this content aligns with how we defined the expected input and output of our function. Inside of the {
and }
region, add the line return x * x;
. Now Visual Studio is happy because it is expecting a return type of double
and we have added code to return a double
, because x is of a double
type.
Code
Your file should look like this. If it doesn't, don't copy! Figure out why yours is different and edit it accordingly.
static double f(double x)
{
return x * x;
}
Call this function from the Main
method, passing in your favorite number for the parameter. Store the output of this function call in a variable and output it to the console. Did it behave as expected? (i.e., did it square the number you passed in?)
Code
Your file should look like this. If it doesn't, don't copy! Figure out why yours is different and edit it accordingly.
double squaredValue = f(4);
Console.WriteLine(squaredValue);
One last thing. "f" is not a descriptive name for a function. Rename it to something useful - how about Square
?
So far we have encountered two operators, =
and *
. =
is the assignment operator that we discussed in Lesson 1. *
is fairly straightforward - it multiplies two numbers together. You can experiment with this operator - try multiplying an int
and a double
, a string
and a double
, etc.
Here are some operators you should know:
+
- add
-
- subtract
*
- multiply
/
- divide
%
- remainder; e.g., 4 % 3
returns 1
.
=
- assignment
(
and )
- used for order of operations; e.g., (3 + 1) * 2
returns 8
.
These operators are fairly straightforward and mostly concern math. There are other operators we'll encounter over the next few lessons.
As you may have noticed, if you have tried to read in a number from the console using the Console.ReadLine function, you cannot store the result in a double
variable; the code will not compile. The Console.ReadLine returns a string
, not a double
.
This can be remedied by first storing the output of Console.ReadLine in a string
, then converting that string
into a double
and storing that double
. You can use the double.Parse
method, passing in a string
and getting a double
as an output. Store the output from that function call in a double
variable. Pass this double
into the Square
method we just wrote. Then write the output of that function out to the console.
- Edit your program so that it can read in a number from the console and write the square of that value to the console.
- When you get that working, run it and imagine that you weren't the one who wrote the program. Imagine you were a user of this program and not its creator. Is clear what the program does? Is it clear what the program is expecting from the user in terms of input? How could you make it more user-friendly instead of user-hostile? Perform those tasks that you can think of.
- If you put in a non-number, what happens? Do you get an error message? If so, does the error message describe the problem well? From a user standpoint, is the error message confusing or helpful?