Functions (or methods) in C# are blocks of reusable code that perform specific tasks. They help organize your code, avoid repetition, and make programs easier to read and maintain.
returnType FunctionName(parameters) {
// function body
return value;
}
Explanation:
returnType
— data type returned by the function (use void
if no value is returned)FunctionName
— your function's nameparameters
— input variables (optional)return
— value returned from the function (if any)int AddNumbers(int a, int b) {
return a + b;
}
This function takes two integers and returns their sum.
Result will appear here...
1. What is the purpose of a function in C#?
2. What keyword defines a function that does not return any value?
3. Which is the correct way to call AddNumbers(5, 10)
?
4. How do you specify a function’s return type in C#?
5. What does this function do?
int MultiplyNumbers(int a, int b) { return a * b; }