0% found this document useful (0 votes)
6 views

C FUNCTION

The document provides an overview of C# functions, detailing their components such as function name, return type, body, access specifier, and parameters. It explains different types of functions including those with and without return types and parameters, as well as concepts like Call By Value, Call By Reference, and Out Parameters. Additionally, it highlights the differences between the 'ref' and 'out' keywords in C# for passing arguments to methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C FUNCTION

The document provides an overview of C# functions, detailing their components such as function name, return type, body, access specifier, and parameters. It explains different types of functions including those with and without return types and parameters, as well as concepts like Call By Value, Call By Reference, and Out Parameters. Additionally, it highlights the differences between the 'ref' and 'out' keywords in C# for passing arguments to methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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:

Function name: It is a unique name that is used to make Function call.

Return type: It is used to specify the data type of function return value.

Body: It is a block that contains executable statements.

Access specifier: It is used to specify function accessibility in the application.

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. }

Access-specifier, parameters and return statement are optional.

Let's see an example in which we have created a function that returns a string value
and takes a string parameter.

C# Function: using no parameter and return type


A function that does not return any value specifies void type as a return type. In the
following example, a function is created without return type.

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:

This is non parameterized function

C# Function: using parameter but no return type

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:

Hello Rahul Kumar

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.

C# Function: using parameter and return type

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:

Inside Show Function


Hello Rahul Kumar

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.

C# Call By Value Example

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:

Value before calling the function 50


Value inside the show function 2500
Value after calling the function 50

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.

C# Call By Reference Example

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.

C# Out Parameter Example 1

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:

Value before passing out variable 50


Value after receiving the out variable 25

The following example demonstrates that how a function can return multiple values.

C# Out Parameter Example 2

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:

Value before passing


val1 = 50
val2 = 100
Value after passing
val1 = 25
val2 = 25

Difference between Ref and Out


keywords in C#

The•• out is a keyword in C# which is used for the passing the arguments to
methods as a reference type. It is generally used when a method returns multiple
values. The out parameter does not pass the property.
Example :
// C# program to illustrate the
// concept of out parameter
using System;
class GFG {

// Main method
static public void Main()
{

// Declaring variable
// without assigning value
int G;

// Pass variable G to the method


// using out keyword
Sum(out G);

// Display the value G


Console.WriteLine("The sum of" +
" the value is: {0}", G);
}

// Method in which out parameter is passed


// and this method returns the value of
// the passed parameter
public static void Sum(out int G)
{
G = 80;
G += 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()
{

// Assign string value


string str = "Geek";

// Pass as a reference parameter


SetValue(ref str);

// Display the given string


Console.WriteLine(str);
}

static void SetValue(ref string str1)


{

// Check parameter value


if (str1 == "Geek") {
Console.WriteLine("Hello!!Geek");
}

// Assign the new value


// of the parameter
str1 = "GeeksforGeeks";
}
}

Output:
Hello!!Geek
GeeksforGeeks
Difference between Ref and Out keywords

ref keyword out keyword

It is necessary the parameters


It is not necessary to initialize parameters before it
should initialize before it pass to
pass to out.
ref.

It is not necessary to initialize the


It is necessary to initialize the value of a
value of a parameter before
parameter before returning to the calling method.
returning to the calling method.

The passing of value through ref


parameter is useful when the The declaring of parameter through out parameter
called method also need to change is useful when a method return multiple values.
the value of passed parameter.

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.

You might also like