EL

User-Defined_Functions_in_C__with_Examples_-_Dot_Net_Tutorials

User-Defined Functions in C#

  • An article discussing user-defined functions in C# Language with examples.

Types of User-Defined Functions

  • There are four types of user-defined functions in C#:

    • Functions with No Argument and No Return Type.

    • Functions with Argument and No Return Type.

    • Functions with No Argument and with Return Type.

    • Functions with Argument and with Return Type.

Functions with No Arguments Passed and No Return Value

  • Definition: A function that does not receive any data from the calling function and does not return a value.

  • Details:

    • No data transfer between calling and called functions.

    • Cannot be used in an expression; only as an independent statement.

Example of No Arguments Passed and No Return Value Function

  • Function: Sum (returns void).

  • Output:

    • "Sum of 10 and 20 is 30"

using System;
namespace FunctionDemo {
  class Program {
    static void Main(string[] args) {
      Sum();
      Console.ReadKey();
    }
    static void Sum() {
      int x = 10;
      int y = 20;
      int sum = x + y;
      Console.WriteLine($"Sum of {x} and {y} is {sum}");
    }
  }
}

Functions with No Arguments Passed but Return a Value

  • Definition: A function that receives no data from the calling function but returns a value.

  • Data Transfer: Data is transferred from called function back to the calling function.

Example of No Arguments Passed but Return a Value Function

  • Function: Sum returns an integer value.

  • Output:

    • "Sum is 30"

using System;
namespace FunctionDemo {
  class Program {
    static void Main(string[] args) {
      int Result = Sum();
      Console.WriteLine($"Sum is {Result}");
      Console.ReadKey();
    }
    static int Sum() {
      int x = 10;
      int y = 20;
      return x + y;
    }
  }
}

Functions with Argument Passed but No Return Value

  • Definition: A function that receives data from the calling function via arguments but does not return a value.

  • Data Transfer: Data is transferred from the calling function to the called function, but not vice versa.

Example of Argument Passed but No Return Value Function

  • Function: Sum receives two integer arguments but returns no value.

  • Output:

    • "Sum is 30"

using System;
namespace FunctionDemo {
  class Program {
    static void Main(string[] args) {
      int x = 10, y = 20;
      Sum(x, y);
      Console.ReadKey();
    }
    static void Sum(int x, int y) {
      int sum = x + y;
      Console.WriteLine($"Sum is {sum}");
    }
  }
}

Functions with Argument Passed and Return Value

  • Definition: A function that receives input via arguments and returns a value, functioning like a “black box.”

  • Data Communication: Two-way data transfer.

Example of Argument Passed and Return Value Function

  • Function: Sum takes two arguments and returns the sum.

  • Output:

    • "Sum is 30"

using System;
namespace FunctionDemo {
  class Program {
    static void Main(string[] args) {
      int x = 10, y = 20;
      int Result = Sum(x, y);
      Console.WriteLine($"Sum is {Result}");
      Console.ReadKey();
    }
    static int Sum(int x, int y) {
      return x + y;
    }
  }
}

Function Overloading in C#

  • Definition: Writing multiple functions with the same name but different argument or parameter lists.

  • Example:

    • add(int x, int y) and add(int x, int y, int z) are valid overloads.

    • Benefit: Simplifies naming and keeps related functionality grouped together.

Example of Function Overloading

  • Function that adds integers:

using System;
namespace FunctionDemo {
    class Program {
        static void Main(string[] args) {
            int a = 10, b = 2;
            int c = add(a, b);
            Console.WriteLine($"Sum of {a} and {b} is {c}");
            int d = add(a, b, c);
            Console.ReadKey();
        }
        static int add(int x, int y) {
            return x + y;
        }
        static int add(int x, int y, int z) {
            return x + y + z;
        }
    }
}
  • Output:

    • "Sum of 10 and 2 is 12"

    • Further check shows multiple overloads work properly based on parameter count.

Conclusion

  • User-defined functions are a key feature in C#, enabling various forms of functionality through arguments and return types. Understanding these concepts is vital for effective programming in C#. Functions can improve code readability, maintainability, and reusability.