Defining Functions - potatoscript/csharp GitHub Wiki
π οΈ Defining Functions in C# π οΈ
π§βπ» What Is a Function? π§βπ»
A function is like a little helper that performs a task for you in your program. Instead of writing the same code over and over again, you can define a function and then call it whenever you need it. Functions help make your code neat, reusable, and easier to manage.
Think of it like a recipe! π§βπ³ When you follow a recipe to bake a cake, you're not repeating the steps each time. You just follow the recipe and get the cake every time. A function is the recipe, and your program is the cake! π
π§βπ» Defining a Function in C# π§βπ»
Hereβs how to define a simple function in C#:
π·οΈ Function Syntax:
returnType FunctionName(parameters)
{
// Code to perform the task
}
returnType
: This is the type of value the function will return (likeint
,string
,void
).FunctionName
: This is the name you give to the function (just like the name of a recipe).parameters
: These are the values that the function needs to work with (like ingredients in a recipe).
π§βπ» A Basic Example: Saying Hello (Void Function) π§βπ»
Letβs start with a very simple function that doesnβt return anything. Weβll call it SayHello
.
π·οΈ Code:
using System;
class Program
{
static void Main()
{
SayHello(); // Calling the SayHello function
}
// Defining a function to say hello
static void SayHello()
{
Console.WriteLine("Hello, welcome to C#!"); // This function just prints a message
}
}
Explanation:
static void SayHello()
: This defines a function calledSayHello
. It has no return value (thatβs why we usevoid
).- Inside the function, we have `Console.WriteLine("Hello, welcome to C#!") which just prints a message to the screen.
- We call the function inside the
Main
method usingSayHello();
.
When you run this program, it will print:
Hello, welcome to C#!
π§βπ» Returning a Value from a Function π§βπ»
You can also define functions that return a value. For example, letβs define a function that adds two numbers together and returns the result.
π·οΈ Code: Adding Two Numbers (Function with Return Value)
using System;
class Program
{
static void Main()
{
int result = AddNumbers(3, 4); // Calling the function and storing the result
Console.WriteLine("The sum is: " + result); // Printing the result
}
// Defining a function that returns the sum of two numbers
static int AddNumbers(int num1, int num2)
{
return num1 + num2; // Adding num1 and num2 and returning the result
}
}
Explanation:
static int AddNumbers(int num1, int num2)
: This defines a function calledAddNumbers
. It takes two parameters,num1
andnum2
, which are the numbers you want to add.- The function returns an
int
(integer) value, which is the sum ofnum1
andnum2
. - We call the function in the
Main
method, passing in 3 and 4, and store the result in the variableresult
.
When you run this program, it will print:
The sum is: 7
π§βπ» Using Parameters in Functions π§βπ»
Parameters are like ingredients in a recipe. You tell the function what you want it to work with by passing values when you call it.
π·οΈ Code: Making a Greeting Function (With Parameters)
using System;
class Program
{
static void Main()
{
GreetUser("Lucy"); // Passing the name "Lucy" to the function
GreetUser("John"); // Passing the name "John" to the function
}
// Defining a function that greets a user by their name
static void GreetUser(string name)
{
Console.WriteLine("Hello, " + name + "! Welcome to the world of C#!");
}
}
Explanation:
- The function
GreetUser
takes astring
parameter calledname
. - When you call
GreetUser("Lucy")
, it passes the name "Lucy" to the function. - The function prints out "Hello, Lucy! Welcome to the world of C#!".
When you run this program, it will print:
Hello, Lucy! Welcome to the world of C#!
Hello, John! Welcome to the world of C#!
π§βπ» Function Overloading π§βπ»
Function Overloading means that you can define multiple functions with the same name, but with different parameters. C# will know which one to use based on the parameters you provide.
π·οΈ Code: Function Overloading (Same Name, Different Parameters)
using System;
class Program
{
static void Main()
{
Console.WriteLine(AddNumbers(3, 4)); // Calling AddNumbers with two integers
Console.WriteLine(AddNumbers(3.5, 4.5)); // Calling AddNumbers with two doubles
}
// Function that adds two integers
static int AddNumbers(int num1, int num2)
{
return num1 + num2;
}
// Function that adds two doubles
static double AddNumbers(double num1, double num2)
{
return num1 + num2;
}
}
Explanation:
- Both functions are named
AddNumbers
, but one adds integers (int
), and the other adds floating-point numbers (double
). - C# automatically chooses the correct function based on the type of values passed when you call it.
When you run this program, it will print:
7
8
π§βπ» Returning Multiple Values from a Function π§βπ»
Sometimes, you might want to return more than one value from a function. While C# doesnβt allow returning multiple values directly, we can use techniques like out parameters or tuples to return multiple values.
π·οΈ Code: Returning Multiple Values Using Tuples
using System;
class Program
{
static void Main()
{
var result = GetFullName("John", "Doe"); // Using a tuple to get both first and last name
Console.WriteLine("Full Name: " + result.Item1 + " " + result.Item2); // Accessing tuple items
}
// Function that returns multiple values using a tuple
static (string, string) GetFullName(string firstName, string lastName)
{
return (firstName, lastName); // Returning both first and last names as a tuple
}
}
Explanation:
- The function
GetFullName
returns a tuple, which is a pair of values: the first name and the last name. - We access the values using
result.Item1
for the first name andresult.Item2
for the last name.
When you run this program, it will print:
Full Name: John Doe