Monday, October 22, 2012

C# for beginners Part 3

Calculator (Functions Or Methods)

Welcome to your third C# for beginners tutorial, in this tutorial you will learn the simple Calculator program after you've learned Hello World! and Hello Me!.

Things you will need:

  1. Microsoft Visual Studio you can download the express edition for free from here free visual studio.
  2. Read Hello Me! if you didn't read it yet From here.
  3. 10 minutes of your time.

Lets start:

Now that you ready to make your third program after installing Microsoft Visual Studio lets open it, now you will have to create a new project there is lots of project's types you can choose from but we will start with the simplest and very basic one of them the Console application.

you will get a window similar to this (it may look different depending on the version of your Visual Studio)

Project Type
Project Type
Choose Visual C# for the programming language then Console Application for the program type.
For the name type Calculator or whatever you like your program name to be, But in here I will be using Calculator as the project name so its a good practice to do the same so that names wont make you confused.

Now that you created the project its time to start writing some codes, This is how your code will look like when you open Program.cs file
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Calculator {     class Program     {         static void Main(string[] args)         {         }     } }

Our program is very simple all we want to do is to make our program ask you to insert a number then second one and Then adds them and give you the result and to make the program read anything from the Console we use Console.ReadLine(); we will also use variables in this program.

NOTE: Variables are containers that hold our data temporary it has many types as we may need to store text or numbers or anything else
text = string       ex:"This is sentence"
number = int      ex: 1
point number = float or decimal       ex: 1.5 

Now lets make our program ask for your first number, In the Main type Console.WriteLine("Please enter the first number");
Then we need to get your number from the Console window so we will declare a variable called num1 of type int and put your number there first here is how to declare a variable of type int

int num1;

its as easy as that but we need  to tell our program to put your number in this variable because now we reserved an integer space in our memory and never used it and this is how to tell your program to read your number from the Console window and put it in the variable num1.

int num1 = int.Parse(Console.ReadLine() );

NOTE: int.Parse() is a simple command that convert the String to a number to be able to put it in an integer variable because Console.ReadLine() returns a String and our variable is int that's why we use it.

NOTE: If the data between int.Parse() brackets isn't a number it will return an error for example int.Parse("a") will return an error also int.Parse("1.1") will return an error.

We will do the same with the other number and create another variable called num2
now all we need to do is to add the two numbers and display the result which is simple as we learned before how to display any text on the Console window but there is a little thing we will add there because we want the program to output num1 + num2 = result so you will notice the difference when we reach this step but now we will make a function or method which are the same.

NOTE: Functions is block of codes to be used in different places in the same code. 

this is how we declare the function

private static string add(int x, int y)


when we declare a function we first set if its going to be public or private which simply means if it private then we cant use it out of this code file if it public then we can, then we declare that its static which will be explained in later tutorials, then we set the return type which means that this function will return a text for me in our case, then we set the function name which can be whatever you want here I called it add, then between the brackets we put our arguments which are values we will be using in the function here I put 2 variables of type int because we will send our 2 numbers to our function.

NOTE: If your function wont return any values you can use void as the return type. 

NOTE: Functions must be declared outside the Main function. 

private static string add(int x, int y)
            int result = x + y;
            return String.Format("{0} + {1} = {2}", x, y, result);

In our function we simple declared another variable which is the result and added our 2 numbers and put the sum in the result variable then we returned the string and we used format to make our output in a specific format which is num1 + num2 = result

Then all we need to do is to call our function and display its return value and this is how

Now this is how your final code will look like
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
    class Program
        static void Main(string[] args)
                 Console.WriteLine("Please enter your first number");
                 int num1 = int.Parse(Console.ReadLine() );
                 Console.WriteLine("Please enter your second number");
                 int num2 = int.Parse(Console.ReadLine() );
        private static string add(int x, int y)
            int result = x + y;
            return String.Format("{0} + {1} = {2}", x, y, result);

Now lets Run our program and see what we did press Ctrl+F5 to run your program

Console Window
The Result

Congratulations now you have done your third C# Program you are ready to learn more

NOTE: Click Download below to download sample project. More Download Links

Download From FileFactory!

Hope you enjoyed and benefited from this tutorial please feel free to comment or ask about anything and more tutorials will be added.
Thank you for reading and enjoy your day. 

1 comment:

  1. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own Blog