Functions and Variables



How to Program

Introduction to Functions and Variables


Ok, so assuming we've installed Visual Studio as described in an earlier post - create a new C# console project with the name of Example1.  This generates some C# code that looks like the following: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Example1
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }
    }
}


Don't worry as to what it all means just yet, the important bit to look at is:


class Program
 {


       static void Main(string[] args)
       {

       }     
  }


This piece of code says that we are making or defining something called a "class" with a name of "Program" and within that class we have a function called "Main".

The Main function is what's called the Entry-Point of the program.  This means that when you load and run the program, this is the first function of your code that gets used.  All the other programming functionality stems from this one Main Function.

Curly Brackets

Curly brackets { } show start and end points - the { bracket after "class Program" is the start of the class definition. Everything included between here and the final closing bracket } is part of the defined class.  Likewise, the { bracket after the "static void Main (string[] args)" is the start of the function "main" whereby everything between this opening bracket and the next closing bracket is part of that function. 

Functions


At the moment this program does nothing - it enters the Main function and leaves without actually doing anything.  Therefore we need to add some code to make it more interesting. If you start typing "Con" then Visual Studio pops up with it's suggestions as to what you might be trying to use - this feature is called Intellisense


Select the word "Console" and it automatically fills in the rest of the word. If you follow this with a full stop, it brings up another intellisense menu with possible options associated with Console.  

Writing wr brings up a menu for "Write" and "WriteLine", we need to use the WriteLine option to produce the following code



  static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }

The WriteLine is a function contained within the Console class that displays text on screen.

The "Hello World" - including the speech marks " " within the normal brackets ( ) is an example of a function parameter. Function parameters are used to call the same function but with a different set of data.  You can add more WriteLine functions to print anything you want.

Clicking the Start button on the menu will run the program - at first it looks as if nothing happens.  If you keep trying however you'll notice that a new window appears and disappears very quickly without hanging around to let you see anything.

The problem is that the system automatically closes the window on program exit and there's nothing in our program to slow it down. Therefore it would be useful to have a way that we can make the program pause until a key is pressed.

Enter "Console.ReadKey();"  after the Console.WriteLine commands. The main function should now look something like the following:

   static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.ReadKey();
        }

If you now click the Start button, the program should create the window and you should see that it displays whatever was sent to the Console.WriteLine functions.  This time however, it is waiting to read a key from the keyboard 






Variables

At the moment this is a bit boring!

The main purpose of all programs is to manipulate data and this is done through the use of things called "Variables".  Variables are called Variables because they can change, alter and "vary" their contents during the lifetime of a program.

Variables need to be defined to tell the C# compiler what type the variable is going to be.  Computable numbers, such as Integers, are treated differently than a string of characters and so declaring the type means that you can't accidently put the wrong type into a variable.

Extend our Example Program to include the following:



 static void Main(string[] args)
        {
            String UserInput;

            Console.WriteLine("Hello World");

            UserInput=Console.ReadLine();

            Console.WriteLine(UserInput);

            Console.ReadKey();
        }


This has declared that we need a string of characters called "UserInput". We use a Console.ReadLine() function to read a string into the variable and then display it back out to the screen. In the following example, I typed "turnip" and so it displayed "turnip" 

Still not very useful!

So after the String definition, enter a new variable called InputLength with a type of "int" - short for integer and then after the Console.ReadLine, enter the following code:

            Console.WriteLine("You typed: " + UserInput);

            InputLength = UserInput.Length;

            Console.WriteLine("Number of characters entered = {0}", InputLength);

            Console.ReadKey();


The output of this program should be something like:



The {0} within the output of WriteLine is a feature that allows WriteLine to insert variables into the string through search and replace.  The first parameter is the string that contains a series of {x} identifiers.  The second and more parameters are then placed into those number identifiers starting at zero.

The integer InputLength can be used as a computable number for a wide range of different operations.

Its also worth noting that all the lines of code finish with a semi-colon.  This is to ensure that the compiler knows where one command finishes and the next command starts.

More information on all these topics can be found at the MSDN Library



===

How Computers work Youtube tutorials





No comments:

Post a Comment