C FUNCTION
C FUNCTION
Function is a block of code that has a signature. Function is used to execute statements
specified in the code block. A function consists of the following components:
Return type: It is used to specify the data type of function return value.
Parameters: It is a list of arguments that we can pass to the function during call.
C# Function Syntax
1. <access-specifier><return-type>FunctionName(<parameters>)
2. {
3. // function body
4. // return statement
5. }
Let's see an example in which we have created a function that returns a string value
and takes a string parameter.
1. using System;
2. namespace FunctionExample
3. {
4. class Program
5. {
6. // User defined function without return type
7. public void Show() // No Parameter
8. {
9. Console.WriteLine("This is non parameterized function");
10. // No return statement
11. }
12. // Main function, execution entry point of the program
13. static void Main(string[] args)
14. {
15. Program program = new Program(); // Creating Object
16. program.Show(); // Calling Function
17. }
18. }
19. }
Output:
1. using System;
2. namespace FunctionExample
3. {
4. class Program
5. {
6. // User defined function without return type
7. public void Show(string message)
8. {
9. Console.WriteLine("Hello " + message);
10. // No return statement
11. }
12. // Main function, execution entry point of the program
13. static void Main(string[] args)
14. {
15. Program program = new Program(); // Creating Object
16. program.Show("Rahul Kumar"); // Calling Function
17. }
18. }
19. }
Output:
A function can have zero or any number of parameters to get data. In the following
example, a function is created without parameters. A function without parameter is
also known as non-parameterized function.
1. using System;
2. namespace FunctionExample
3. {
4. class Program
5. {
6. // User defined function
7. public string Show(string message)
8. {
9. Console.WriteLine("Inside Show Function");
10. return message;
11. }
12. // Main function, execution entry point of the program
13. static void Main(string[] args)
14. {
15. Program program = new Program();
16. string message = program.Show("Rahul Kumar");
17. Console.WriteLine("Hello "+message);
18. }
19. }
20. }
Output:
C# Call By Value
In C#, value-type parameters are that pass a copy of original value to the function
rather than reference. It does not modify the original value. A change made in passed
value does not alter the actual value. In the following example, we have pass value
during function call.
1. using System;
2. namespace CallByValue
3. {
4. class Program
5. {
6. // User defined function
7. public void Show(int val)
8. {
9. val *= val; // Manipulating value
10. Console.WriteLine("Value inside the show function "+val);
11. // No return statement
12. }
13. // Main function, execution entry point of the program
14. static void Main(string[] args)
15. {
16. int val = 50;
17. Program program = new Program(); // Creating Object
18. Console.WriteLine("Value before calling the function "+val);
19. program.Show(val); // Calling Function by passing value
20. Console.WriteLine("Value after calling the function " + val);
21. }
22. }
23. }
Output:
C# Call By Reference
C# provides a ref keyword to pass argument as reference-type. It passes reference of
arguments to the function rather than copy of original value. The changes in passed
values are permanent and modify the original variable value.
1. using System;
2. namespace CallByReference
3. {
4. class Program
5. {
6. // User defined function
7. public void Show(ref int val)
8. {
9. val *= val; // Manipulating value
10. Console.WriteLine("Value inside the show function "+val);
11. // No return statement
12. }
13. // Main function, execution entry point of the program
14. static void Main(string[] args)
15. {
16. int val = 50;
17. Program program = new Program(); // Creating Object
18. Console.WriteLine("Value before calling the function "+val);
19. program.Show(ref val); // Calling Function by passing reference
20. Console.WriteLine("Value after calling the function " + val);
21. }
22. }
23. }
Output:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 2500
C# Out Parameter
C# provides out keyword to pass arguments as out-type. It is like reference-type,
except that it does not require variable to initialize before passing. We must
use out keyword to pass argument as out-type. It is useful when we want a function
to return multiple values.
1. using System;
2. namespace OutParameter
3. {
4. class Program
5. {
6. // User defined function
7. public void Show(out int val) // Out parameter
8. {
9. int square = 5;
10. val = square;
11. val *= val; // Manipulating value
12. }
13. // Main function, execution entry point of the program
14. static void Main(string[] args)
15. {
16. int val = 50;
17. Program program = new Program(); // Creating Object
18. Console.WriteLine("Value before passing out variable " + val);
19. program.Show(out val); // Passing out argument
20. Console.WriteLine("Value after recieving the out variable " + val);
21. }
22. }
23. }
Output:
The following example demonstrates that how a function can return multiple values.
1. using System;
2. namespace OutParameter
3. {
4. class Program
5. {
6. // User defined function
7. public void Show(out int a, out int b) // Out parameter
8. {
9. int square = 5;
10. a = square;
11. b = square;
12. // Manipulating value
13. a *= a;
14. b *= b;
15. }
16. // Main function, execution entry point of the program
17. static void Main(string[] args)
18. {
19. int val1 = 50, val2 = 100;
20. Program program = new Program(); // Creating Object
21. Console.WriteLine("Value before passing \n val1 = " + val1+" \n val2 =
"+val2);
22. program.Show(out val1, out val2); // Passing out argument
23. Console.WriteLine("Value after passing \n val1 = " + val1 + " \n val2 =
" + val2);
24. }
25. }
26. }
Output:
// Main method
static public void Main()
{
// Declaring variable
// without assigning value
int G;
Output:
The sum of the value is: 160
The ref is a keyword in C# which is used for the passing the arguments by a
reference. Or we can say that if any changes made in this argument in the method
will reflect in that variable when the control return to the calling method..
Example:
// C# program to illustrate the
// concept of ref parameter
using System;
class GFG {
// Main Method
public static void Main()
{
Output:
Hello!!Geek
GeeksforGeeks
Difference between Ref and Out keywords
When ref keyword is used the When out keyword is used the data only passed in
data may pass in bi-directional. unidirectional.
Note: Both ref and out parameter treated same at compile-time but different at
run-time.