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

4.Methods and Functions

The document provides an overview of methods and functions in C#, explaining their definitions, differences, and how to create and call them. It covers method signatures, parameters, method overloading, and recursion, including its advantages and disadvantages. The content aims to enhance code organization, readability, and maintainability through the use of methods and functions.

Uploaded by

mahdiware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

4.Methods and Functions

The document provides an overview of methods and functions in C#, explaining their definitions, differences, and how to create and call them. It covers method signatures, parameters, method overloading, and recursion, including its advantages and disadvantages. The content aims to enhance code organization, readability, and maintainability through the use of methods and functions.

Uploaded by

mahdiware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

C# Programming

Ch4: Methods And Functions


RSU– BOSASO, SOMALIA
Methods

 A method is a block of code that performs a specific task. It


can be executed when called.
 Methods can be defined within classes and are used to break
down complex programs into simpler.

 Methods improve code organization, readability, and


maintainability by promoting code reuse.
Functions

 a function is defined as a technique of wrapping code


to perform a certain task and then return a value.
 It is quite the same as a method and sometimes both
the terms are used interchangeably.
 The basic difference between Methods and Functions is
that the method comes with a void as return type
whereas function has a return type.
Create a Method

A method is defined with the name of the method, followed by parentheses (). C#
provides some pre-defined methods, which you already are familiar with, such
as Main(), but you can also create your own methods to perform certain actions.
Example

Create a method inside the Program class

class Program {
static void MyMethod()
{
// code to be executed } }
Create a Method

•MyMethod() is the name of the method


•static means that the method belongs to the Program class and
not an object of the Program class.
•void means that this method does not have a return value.
Declaring a Method
 The method signature consists of the method name and its parameter list. It
uniquely identifies the method within its class. Below image is how we define a
signature of the method.
The method signature consists of the method name and its parameter
list. It uniquely identifies the method within its class. Below image is how
we define a signature of the method.
Declaring a Method

•Access_Specifier - It is used to define an access level,


either public or private, etc., to allow other classes to access the
method. If we didn’t mention any access modifier, then by default, it
is private.

•Return_Type - It is used to specify the type of value the method can


return. If the method is not returning any value, then we need to
mention void as the return type.

•Method_Name - It must be a unique name to identify the method in a


class.

•Parameters - The method parameters are useful to send or receive data


from a method, and these method parameters are enclosed within
parentheses and are separated by commas. If no parameters are required
for a method, we need to define a method with empty parentheses.
Call a Method

To call (execute) a method, write the method's name followed by two


parentheses () and a semicolon;
In the following example, MyMethod() is used to print a text (the action), when it is
called:
Example

static void MyMethod()


{
Console.WriteLine("I just got executed!");
}
static void Main(string[] args)
{
MyMethod();
}
// Outputs "I just got executed!"
Parameters and Arguments

Methods can accept parameters to perform operations based on input values


Below is a table summarizing different types of
parameters:
A Function With A Parameter
But No Return Type

 So we can create a function by providing some parameters


without returning anything.

A Function With both A Parameter and


Return Type

 As well we can create a function which can have both


parameters and returning type.
Method Overloading

 With method overloading, multiple methods can have the same


name with different parameters:

Example

int MyMethod(int x)
float MyMethod(float x)
double MyMethod(double x, double y)
Recursion method

Recursion is a function that calls itself. Or in other words, recursion is a


process where a function calls itself repeatedly until some specified
conditions have been completed.

Two conditions must be satisfied if we want to solve a problem


recursively. First, the problem must be written in recursive form so that
the function will call itself, and the second condition is that the problem
statement must include a stopping condition so we can stop the
function call.
Advantage of Recursion

 Recursion will keep track of information based on


the function calls.
 Recursion will be used to evaluate the stack.
 Recursion will be used to evaluate the prefix,
postfix, and infix notations.
Dis-advantage of Recursion

 Due to stack overlap, the process is extremely slow.


 Stack overflow can be produced by the recursive
program crush.
 Infinite loops can be made by the recursive
program.
End

You might also like