0% found this document useful (0 votes)
8 views5 pages

7 Function

The document provides an overview of functions in C#, including their definition, syntax, and various types such as simple functions, functions with parameters, return values, default parameters, function overloading, and recursive functions. It includes examples demonstrating how to define, call, and utilize functions for different tasks such as calculating factorials, checking prime numbers, and manipulating arrays. Additionally, it covers advanced topics like variable arguments and calculating areas, making it a comprehensive guide to C# functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

7 Function

The document provides an overview of functions in C#, including their definition, syntax, and various types such as simple functions, functions with parameters, return values, default parameters, function overloading, and recursive functions. It includes examples demonstrating how to define, call, and utilize functions for different tasks such as calculating factorials, checking prime numbers, and manipulating arrays. Additionally, it covers advanced topics like variable arguments and calculating areas, making it a comprehensive guide to C# functions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Functions in C#

1. What is a Function?
- A function is a reusable block of code that performs a specific task.
- Functions in C are called methods.

2. Defining a Function
- Syntax:

returnType FunctionName(parameters)
{
// Code to execute
return value; // If applicable
}

3. Calling a Function
- Use the function name followed by parentheses:

FunctionName(arguments);

4. Return Types
- Functions can return values (e.g., int, string) or void for no return.

5. Parameters
- Functions can take inputs as parameters:

void Greet(string name)


{
Console.WriteLine($"Hello, {name}!");
}

6. Default Parameters
- Provide default values for parameters:

void Greet(string name = "Guest")


{
Console.WriteLine($"Hello, {name}!");
}

7. Function Overloading
- Define multiple functions with the same name but different parameters.

8. Recursive Functions
- A function that calls itself:

int Factorial(int n)
{
return n == 1 ? 1 : n Factorial(n - 1);
}
1. Simple Function Without Parameters

void SayHello()
{
Console.WriteLine("Hello, World!");
}
SayHello(); // Outputs: Hello, World!

2. Function with Parameters

void Greet(string name)


{
Console.WriteLine($"Hello, {name}!");
}
Greet("Alice"); // Outputs: Hello, Alice!

3. Function with Return Value

int Add(int a, int b)


{
return a + b;
}
Console.WriteLine(Add(5, 3)); // Outputs: 8

4. Function Returning a String

string GetGreeting()
{
return "Welcome!";
}
Console.WriteLine(GetGreeting()); // Outputs: Welcome!

5. Function with Default Parameters

void PrintMessage(string message = "Hello!")


{
Console.WriteLine(message);
}
PrintMessage(); // Outputs: Hello!
PrintMessage("Hi!"); // Outputs: Hi!

6. Function to Multiply Two Numbers

int Multiply(int x, int y)


{
return x y;
}
Console.WriteLine(Multiply(4, 5)); // Outputs: 20

7. Function to Find Maximum

int Max(int a, int b)


{
return a > b ? a : b;
}
Console.WriteLine(Max(10, 20)); // Outputs: 20

8. Function to Check Even or Odd

bool IsEven(int num)


{
return num % 2 == 0;
}
Console.WriteLine(IsEven(4)); // Outputs: True

9. Function to Calculate Square

int Square(int num)


{
return num num;
}
Console.WriteLine(Square(6)); // Outputs: 36

10. Function to Print an Array

void PrintArray(int[] arr)


{
foreach (int item in arr)
{
Console.WriteLine(item);
}
}
PrintArray(new int[] { 1, 2, 3 });
// Outputs: 1 2 3

11. Function to Reverse a String

string ReverseString(string str)


{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
Console.WriteLine(ReverseString("C")); // Outputs: C

12. Recursive Function to Calculate Factorial

int Factorial(int n)
{
return n == 1 ? 1 : n Factorial(n - 1);
}
Console.WriteLine(Factorial(5)); // Outputs: 120

13. Function to Check Prime Number

bool IsPrime(int num)


{
for (int i = 2; i < num; i++)
{
if (num % i == 0) return false;
}
return num > 1;
}
Console.WriteLine(IsPrime(7)); // Outputs: True

14. Function to Calculate Area of a Circle

double CircleArea(double radius)


{
return Math.PI radius radius;
}
Console.WriteLine(CircleArea(5)); // Outputs: 78.5398163397448

15. Function Overloading Example

void Print(int value) => Console.WriteLine(value);


void Print(string value) => Console.WriteLine(value);

Print(10); // Outputs: 10
Print("Hello!"); // Outputs: Hello!

16. Function to Convert Celsius to Fahrenheit

double CelsiusToFahrenheit(double celsius)


{
return (celsius 9 / 5) + 32;
}
Console.WriteLine(CelsiusToFahrenheit(0)); // Outputs: 32

17. Function to Count Words in a Sentence

int WordCount(string sentence)


{
return sentence.Split(' ').Length;
}
Console.WriteLine(WordCount("Hello C World")); // Outputs: 3

18. Function to Find Minimum in Array

int FindMin(int[] arr)


{
return arr.Min();
}
Console.WriteLine(FindMin(new int[] { 4, 2, 7 })); // Outputs: 2

19. Function to Sort an Array

int[] SortArray(int[] arr)


{
Array.Sort(arr);
return arr;
}
int[] sorted = SortArray(new int[] { 3, 1, 2 });
Console.WriteLine(string.Join(", ", sorted)); // Outputs: 1, 2, 3

20. Function with Variable Arguments

void PrintNames(params string[] names)


{
foreach (string name in names)
{
Console.WriteLine(name);
}
}
PrintNames("Alice", "Bob", "Charlie");
// Outputs: Alice Bob Charlie

You might also like