locked
Difference b/w function and subroutine? RRS feed

  • Question

  • What is difference between a subroutine and a function and a method. i am very confused by the these three terms. some says they are alike and some says they are different.
    Tuesday, October 9, 2012 3:25 PM

Answers

  • C# does not have true functions and subroutines. But VB has them. C# knows method(s), but function (in VB) and method (in C#) are virtually the same thing.

    Read this David`s post here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/20f292bb-8d8c-4f1e-9ddc-28d68955a418/


    Mitja

    • Proposed as answer by JohnGrove Tuesday, October 9, 2012 4:19 PM
    • Marked as answer by zain_zone Tuesday, October 9, 2012 5:17 PM
    Tuesday, October 9, 2012 3:57 PM
  • usually in C# both, subroutine and function, is now referred to as method.

    however, in vb and c# both keyword is used differently, but same concept applicable to each in comparison to c#... again now refers to as 'method'.

    sample VB function:

        Function MyFunction(ByVal param As IntegerAs String 'Sample function
     
            Return "TestString" 'this value will be return from the calling line
        End Function
     
        'and you would call this by using a variable and assigning its' value with the function name as in
        Sub Call_The_Function() 'sample subroutine
            Dim m As String = Nothing
     
            m = MyFunction(1)
            'after the line above execute, the value of "m" is "TestString"
        End Sub

    basically, subroutine is executing code blocks without returning some sort of value (i.e. object, array, int, string, etc...)

    as for function, it returns some value... again the type depends on how it is declared.

    in C#... functions and sub is now referred as 'method' but again it can or does not have to return some value.

    • Marked as answer by zain_zone Tuesday, October 9, 2012 5:17 PM
    Tuesday, October 9, 2012 4:08 PM

All replies

  • C# does not have true functions and subroutines. But VB has them. C# knows method(s), but function (in VB) and method (in C#) are virtually the same thing.

    Read this David`s post here: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/20f292bb-8d8c-4f1e-9ddc-28d68955a418/


    Mitja

    • Proposed as answer by JohnGrove Tuesday, October 9, 2012 4:19 PM
    • Marked as answer by zain_zone Tuesday, October 9, 2012 5:17 PM
    Tuesday, October 9, 2012 3:57 PM
  • usually in C# both, subroutine and function, is now referred to as method.

    however, in vb and c# both keyword is used differently, but same concept applicable to each in comparison to c#... again now refers to as 'method'.

    sample VB function:

        Function MyFunction(ByVal param As IntegerAs String 'Sample function
     
            Return "TestString" 'this value will be return from the calling line
        End Function
     
        'and you would call this by using a variable and assigning its' value with the function name as in
        Sub Call_The_Function() 'sample subroutine
            Dim m As String = Nothing
     
            m = MyFunction(1)
            'after the line above execute, the value of "m" is "TestString"
        End Sub

    basically, subroutine is executing code blocks without returning some sort of value (i.e. object, array, int, string, etc...)

    as for function, it returns some value... again the type depends on how it is declared.

    in C#... functions and sub is now referred as 'method' but again it can or does not have to return some value.

    • Marked as answer by zain_zone Tuesday, October 9, 2012 5:17 PM
    Tuesday, October 9, 2012 4:08 PM
  • As Mitja has stated in C# both subroutine and functions (VB terms) are called 'methods'. Only difference between a subroutine and a function is a function returns a value while a subroutine performs some action.

    Example:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //this returns a value (result)
                Int32 result = Add(8977, 2567);
    
                //This displays the value
                DisplayResults(result);
    
                Console.ReadLine();
    
            }
    
    
            //Notice this returns a value
            static Int32 Add(Int32 number1, Int32 number2)
            {
                return (number1 + number2);
            }
    
            static void DisplayResults(Int32 results)
            {
                Console.WriteLine(results);
            }
        }
    }


    • Edited by JohnGrove Tuesday, October 9, 2012 4:31 PM
    Tuesday, October 9, 2012 4:30 PM
  • Before OOP became popular programs would often be made up of code that called a number of subroutines. Subroutines were blocks of named code. Some subroutines returned a value, others did not. Those that returned a value were often called functions; those that did not were some times called procedures (e.g. in Pascal). So subroutine generally means a block of code called by other code.

    In the world of OOP, the term method is generally used to mean a subroutine that is part of a class. So, generally, a method is a subroutine that applies to an instance of a class (but you can have class level methods - static or shared).

    When it comes to different programming languages different ways are used to distinguish between subroutines/methods that return values and those that don't. VB uses keywords Sub to mean no value is returned and Function to mean one is returned. C# has no keyword; all methods return a value of some type. If the type is void then nothing is returned (i.e. it's same as VB Sub); otherwise it's like a VB Function.

    Roughly, subroutine is the same as method; functions are subroutines/methods that return a value. But the actual meaning depends on context.


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Tuesday, October 9, 2012 4:37 PM
  • //Advanced comparison

    char a; char c;

    class myObj obj;

    obj = updateObjectFuction(a, c); //Function //Returns "NEW" object

    obj = new myObj(); updateObjectMethod(ref obj, a, c); //Method simular to Function //Returns null and Edits "REF" object

    public myObj updateObjectFunction(int a, int c) //Function

    {
    myObj ac = new myObj();

    ac.a = a;

    ac.c = c;

    ac.Total = a+c;

    return ac;

    }

    public void updateObjectMethod(ref myObj ac, int a, int c) //Function

    {
    ac.a = a;

    ac.c = c;

    ac.Total = a + c;

    //return null;

    }

    public class myObj

    {

    public int a;

    public int b;

    public int Total;

    }

    Not much difference in outcome.

    Just a different syntax.

    Tuesday, October 9, 2012 8:45 PM