Getting Started With F# in Visual Studio 2012

Introduction

This article will help you to understand the basics of F# programming language in Visual Studio 2012. This article is for web developers that have never worked with the F# programming language. The F# programming language supports OOP (Object Oriented Programming) and the Functional programming language.

What is F#?

F# is a functional programming language supporting the .NET Framework.  F# has access to the .NET Framework APIs and the other .NET languages can use F# libraries. F# programming uses basically a combination of Functional Programming as well as Imperative and Object Oriented Programming concepts. F# is a language that can take advantage of core libraries like Windows Communication Foundations (WCF), Windows Presentation Foundations (WPF) and so on. F# runs on the CLR, embraces Object Oriented Programming, and has features to ensure a smooth integration with the .NET Framework. F# has its root in a programming language called OCaml and has the capability to cross-compile OCaml code, which means that F# can compile simple OCaml programs without modification.

Paradigm of F#

F# is a combination of two separate types of paradigms such as:

  • Object Oriented Programming.
  • Functional Programming.
  • Imperative Programming.

Object Oriented Programming

In an Object Oriented Programming everything resides in an object. F# uses object Object Oriented Programming to implement the user interface. The Object Oriented Programming environment is reusable, not only by the program for which it is initially created but also by other object-oriented programs (and, for this reason, can be more easily distributed for use in networks).

Functional Programming

Functional programming makes programming more like mathematics by making programming functions more like mathematical functions. A Functional programming environment does not have the ability to store the state of an operation. Once the value has been initialized, the variable cannot be changed in programs, in other words variables in functional programming are immutable.

Imperative Programming

f# uses imperative programming features suchas:

  • for loops.
  • while loops.
  • hash table.
  • arrays.

Features of f#

  • Asynchronous and Parallel Programming.
  • Support for Functional Programming.
  • Support for Object Oriented Programming.
  • Support for Mathematical Programming.
  • Pattern Matching.
  • Lambda Expressions.

How to create your first program in F#

Step 1: Open Visual Studio 2012 and click "New project".

Newproject.jpg

Step 2: Select F# and click the "Ok" button.

MyFirstApp.jpg

Step 3: Enter the following code in the Program.fs file:

#light

open System

System.Console.Write("Enter the first number:");

let num1= System.Console.ReadLine();

System.Console.Write("Enter the second number:");

let num2= System.Console.ReadLine();

let num3= System.Convert.ToInt32(num1)+ System.Convert.ToInt32(num2);

let num4=System.Convert.ToInt32(num1)* System.Convert.ToInt32(num2);

let num5=System.Convert.ToInt32(num1)/ System.Convert.ToInt32(num2);

let num6=System.Convert.ToInt32(num1)% System.Convert.ToInt32(num2);

let num7=System.Convert.ToInt32(num1)- System.Convert.ToInt32(num2);

printfn "Addition of two number is:%d" num3;

printfn "Multiplication of two number is:%d" num4;

printfn "Division of two number is:%d" num5;

printfn "Modulus of two number is:%d" num6;

printfn "Subtraction of two number is:%d" num7;

System.Console.ReadKey(true);

Step 4: Press F5 to compile the application.

EnterTwoNumber.jpg

Result.jpg

Summary

In this article I have explained F# in Visual Studio 2012. This article explains the features of the F# programming language used in Visual Studio. F# is a functional programming language. I hope this article helps you to understand the F# programming language.

 


Similar Articles