Last saved 21 days ago
EL

Functions in C# with Examples - Dot Net Tutorials

Functions in C#

  • Groups of related instructions that perform specific tasks, allowing code reusability and simplifying coding tasks.

  • Take input as parameters and return a result as a return value.

Importance of Functions

  • Break complex programs into manageable pieces.

  • Each function handles specific tasks, enhancing code readability and maintainability.

  • Organizes code and prevents redundancy.

Monolithic Programming

  • Definition: All code resides in a single main function.

  • Problems:

    • Errors in one line can disrupt the entire program.

    • Larger codebases become cumbersome to manage and understand.

    • Limits collaboration as only one person can work on the single main function.

    • Large programs may exceed memory limits.

Modular Programming

  • Definition: Programs broken into smaller, reusable functions.

  • Benefits:

    • Facilitates piece-wise development.

    • Encourages teamwork as different programmers can work on separate functions.

    • Increases code productivity and reusability.

Types of Functions in C#

  • Built-in Functions:

    • Pre-defined functions in the C# framework.

    • Optimized for performance and saves development time.

  • User-Defined Functions:

    • Defined by developers for specific tasks; allows customization and reduces program complexity.

Limitations of Built-in Functions

  • Limited tasks; may necessitate user-defined functions for specific needs.

Creating User-Defined Functions in C#

  • Naming: Mandatory, follows variable naming rules.

  • Parameter List: Optional, can take multiple parameters or none.

  • Return Type: Mandatory, defines type of value returned. A 'void' return type indicates no return value.

  • Access Specifier: Optional, outlines method accessibility (e.g., public, private).

  • Modifier: Optional, controls access type (e.g., static, virtual).

Example of User-Defined Function

using System; namespace FunctionDemo { class Program { static void Main(string[] args) { int x = 10; int y = 15; int sum = Add(x, y); Console.WriteLine($"Sum is {sum}"); Console.ReadKey(); } static int Add(int a, int b) { return a + b; } } }
  • In this example, the Add function takes two integers, computes their sum, and returns it to the calling function.

Function Parameters

  • Actual Parameters: Values passed to functions (e.g., x, y in the example).

  • Formal Parameters: Variables in the function signature (e.g., a, b in Add function).

  • Actual parameters are copied into formal parameters during the function call.

Memory Management in Functions

  • Each method has its own activation area; variables inside a function can't access variables of outside functions.

  • Effective memory handling enhances performance and organization.

Return Statement in C#

  • Terminates function execution and returns control to the calling function.

  • Can return a value to the calling function.

  • Used when the function is ready to return a value.

How to Call a Method in C#?

  • Invoking a method requests an action (e.g., setting a value, printing statements, performing calculations).

  • Code contains the method name and any necessary data specified in the parameter list.

  • Control transfers to the called method, which returns control under three conditions:

    1. When the return statement is executed.

    2. When it reaches the closing curly brace.

    3. When it throws an unhandled exception.


robot
knowt logo

Functions in C# with Examples - Dot Net Tutorials

Functions in C#

  • Groups of related instructions that perform specific tasks, allowing code reusability and simplifying coding tasks.

  • Take input as parameters and return a result as a return value.

Importance of Functions

  • Break complex programs into manageable pieces.

  • Each function handles specific tasks, enhancing code readability and maintainability.

  • Organizes code and prevents redundancy.

Monolithic Programming

  • Definition: All code resides in a single main function.

  • Problems:

    • Errors in one line can disrupt the entire program.

    • Larger codebases become cumbersome to manage and understand.

    • Limits collaboration as only one person can work on the single main function.

    • Large programs may exceed memory limits.

Modular Programming

  • Definition: Programs broken into smaller, reusable functions.

  • Benefits:

    • Facilitates piece-wise development.

    • Encourages teamwork as different programmers can work on separate functions.

    • Increases code productivity and reusability.

Types of Functions in C#

  • Built-in Functions:

    • Pre-defined functions in the C# framework.

    • Optimized for performance and saves development time.

  • User-Defined Functions:

    • Defined by developers for specific tasks; allows customization and reduces program complexity.

Limitations of Built-in Functions

  • Limited tasks; may necessitate user-defined functions for specific needs.

Creating User-Defined Functions in C#

  • Naming: Mandatory, follows variable naming rules.

  • Parameter List: Optional, can take multiple parameters or none.

  • Return Type: Mandatory, defines type of value returned. A 'void' return type indicates no return value.

  • Access Specifier: Optional, outlines method accessibility (e.g., public, private).

  • Modifier: Optional, controls access type (e.g., static, virtual).

Example of User-Defined Function

using System;  
namespace FunctionDemo {
class Program {
static void Main(string[] args) {
int x = 10;
int y = 15;
int sum = Add(x, y);
Console.WriteLine($"Sum is {sum}");
Console.ReadKey();
}
static int Add(int a, int b) {
return a + b;
}
}
}
  • In this example, the Add function takes two integers, computes their sum, and returns it to the calling function.

Function Parameters

  • Actual Parameters: Values passed to functions (e.g., x, y in the example).

  • Formal Parameters: Variables in the function signature (e.g., a, b in Add function).

  • Actual parameters are copied into formal parameters during the function call.

Memory Management in Functions

  • Each method has its own activation area; variables inside a function can't access variables of outside functions.

  • Effective memory handling enhances performance and organization.

Return Statement in C#

  • Terminates function execution and returns control to the calling function.

  • Can return a value to the calling function.

  • Used when the function is ready to return a value.

How to Call a Method in C#?

  • Invoking a method requests an action (e.g., setting a value, printing statements, performing calculations).

  • Code contains the method name and any necessary data specified in the parameter list.

  • Control transfers to the called method, which returns control under three conditions:

    1. When the return statement is executed.

    2. When it reaches the closing curly brace.

    3. When it throws an unhandled exception.