SlideShare a Scribd company logo
C# Function
What is a Function?
A function is a named, independent section of C# code that performs a
specific task and optionally returns a value to the calling program.
SYNTAX:
<access_modifier> <return_type> <function_name> (parameters)
{
body
}
Access modifiers
- keywords used to specify accessibility of a member or a type. An access modifier
allows us a way to handle which member or type has an access or not has an access
to certain features in a program.
Function return type
- specifies the data type that the function returns to the calling program. The return
type can be of C#’s data types: char, int, long, float, or double. You can also define a
function that doesn’t return a value, a return type of void. Here are some examples:
int func1(. . .) /* return a type int */
float func2(. . .) /* return a type float */
void func3(. . .) /* returns nothing */
Function name
You can name a function anything you like, as long as you follow
the rules for C# variable names. A function name must be unique
(not assigned to any other function or variable). It’s a good idea
to assign a name that reflects what the function does.
An entry in a function header; it serves as a “place holder” for an
argument. A function’s parameters are fixed; they do not change during
program execution.
Parameter
an actual value passed to the function by the calling program. Each time a function is
called, it can be passed different arguments.
Argument
Where to Put Functions?
/* start of source code */
. . .
function1( )
{
. . .
}
function2( )
{
. . .
}
static void Main(string [] args)
{
. . .
}
/* end of source code */
Example 1: Void and Parameterless Function
static public void PrintMyName() //function definition
{
Console.WriteLine(“Jose Protacio Rizal Mercado y Alonzo Realonda”);
}
static void Main(string [] args)
{
PrintMyName(); //function call
Console.ReadKey(true);
}
Example 2:Functions with Return Value and Parameters
static int Sum(int x, int y)
{
return x + y;
}
static void Main(string [] args)
{
int number1 = 5;
int number2 = 10;
int total;
total = Sum(number1, number2);
Console.WriteLine(“Sum is: “ + total);
Console.ReadKey(true);
}
Example 3: A program that will calculate the cube of a number
static long cube(long x)
{
long x_cube;
x_cube = x*x*x;
return x_cube;
}
static void Main(string [] args)
{
long input, answer;
Console.Write(“Enter an integer value: “);
input = Convert.ToInt64(Console.ReadLine());
answer = cube(input);
Console.WriteLine(“The cube of “ + input + “is “ + answer);
Console.ReadKey(true);
}
Example 4: Finding Maximum Value
using System;
namespace CalculatorApplication
{
class NumberManipulator
{
public int FindMax(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
static void Main(string[] args)
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
NumberManipulator n = new NumberManipulator();
//calling the FindMax method
ret = n.FindMax(a, b);
Console.WriteLine("Max value is : {0}", ret );
Console.ReadLine();
}
}
}
Example 5: A method can call itself. This is known as recursion.
Recursion is made for solving problems that can be broken
down into smaller, repetitive problems. It is especially good for
working on things that have many possible branches and are
too complex for an iterative approach. Following is an example
that calculates factorial for a given number using a recursive
function:
using System;
namespace CalculatorApplication
{
class NumberManipulator
{
public int factorial(int num)
{
/* local variable declaration */
int result;
if (num == 1)
{
return 1;
}
else
{
result = factorial(num - 1) * num;
return result;
}
}
static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
//calling the factorial method
Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6));
Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
Console.ReadLine();
}
}
}

More Related Content

PPTX
C function
PPTX
Unit-III.pptx
PPTX
USER DEFINE FUNCTION AND STRUCTURE AND UNION
PPTX
Dti2143 chapter 5
PPT
Lecture 4
PPTX
Function in c program
PPTX
C and C++ functions
C function
Unit-III.pptx
USER DEFINE FUNCTION AND STRUCTURE AND UNION
Dti2143 chapter 5
Lecture 4
Function in c program
C and C++ functions

Similar to Computer-programming-User-defined-function-1.pptx (20)

PPTX
Silde of the cse fundamentals a deep analysis
PPTX
Function in C program
PDF
Unit 3 (1)
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PDF
FUNCTIONS IN C PROGRAMMING.pdf
PPTX
Functionincprogram
PPTX
Module 3-Functions
DOC
Functions struct&union
PPTX
Lecture 1_Functions in C.pptx
PDF
Functions
PPTX
Fundamentals of functions in C program.pptx
PPTX
unit_2.pptx
PPTX
unit_2 (1).pptx
PPT
function_v1.ppt
PPT
function_v1.ppt
PPT
Inbuilt Functions in C++ computer language.ppt
PPT
Fp201 unit5 1
DOC
Unit 4 (1)
PPTX
1. DSA - Introduction.pptx
PPT
arrays.ppt
Silde of the cse fundamentals a deep analysis
Function in C program
Unit 3 (1)
Chp8_C++_Functions_Part2_User-defined functions.pptx
FUNCTIONS IN C PROGRAMMING.pdf
Functionincprogram
Module 3-Functions
Functions struct&union
Lecture 1_Functions in C.pptx
Functions
Fundamentals of functions in C program.pptx
unit_2.pptx
unit_2 (1).pptx
function_v1.ppt
function_v1.ppt
Inbuilt Functions in C++ computer language.ppt
Fp201 unit5 1
Unit 4 (1)
1. DSA - Introduction.pptx
arrays.ppt
Ad

Recently uploaded (20)

PDF
Insiders guide to clinical Medicine.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Structure & Organelles in detailed.
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Institutional Correction lecture only . . .
PPTX
master seminar digital applications in india
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Computing-Curriculum for Schools in Ghana
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Insiders guide to clinical Medicine.pdf
Complications of Minimal Access Surgery at WLH
Cell Structure & Organelles in detailed.
VCE English Exam - Section C Student Revision Booklet
Institutional Correction lecture only . . .
master seminar digital applications in india
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
Renaissance Architecture: A Journey from Faith to Humanism
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPH.pptx obstetrics and gynecology in nursing
Computing-Curriculum for Schools in Ghana
102 student loan defaulters named and shamed – Is someone you know on the list?
Ad

Computer-programming-User-defined-function-1.pptx

  • 2. What is a Function? A function is a named, independent section of C# code that performs a specific task and optionally returns a value to the calling program. SYNTAX: <access_modifier> <return_type> <function_name> (parameters) { body }
  • 3. Access modifiers - keywords used to specify accessibility of a member or a type. An access modifier allows us a way to handle which member or type has an access or not has an access to certain features in a program. Function return type - specifies the data type that the function returns to the calling program. The return type can be of C#’s data types: char, int, long, float, or double. You can also define a function that doesn’t return a value, a return type of void. Here are some examples: int func1(. . .) /* return a type int */ float func2(. . .) /* return a type float */ void func3(. . .) /* returns nothing */
  • 4. Function name You can name a function anything you like, as long as you follow the rules for C# variable names. A function name must be unique (not assigned to any other function or variable). It’s a good idea to assign a name that reflects what the function does. An entry in a function header; it serves as a “place holder” for an argument. A function’s parameters are fixed; they do not change during program execution. Parameter
  • 5. an actual value passed to the function by the calling program. Each time a function is called, it can be passed different arguments. Argument
  • 6. Where to Put Functions? /* start of source code */ . . . function1( ) { . . . } function2( ) { . . . } static void Main(string [] args) { . . . } /* end of source code */
  • 7. Example 1: Void and Parameterless Function static public void PrintMyName() //function definition { Console.WriteLine(“Jose Protacio Rizal Mercado y Alonzo Realonda”); } static void Main(string [] args) { PrintMyName(); //function call Console.ReadKey(true); }
  • 8. Example 2:Functions with Return Value and Parameters static int Sum(int x, int y) { return x + y; } static void Main(string [] args) { int number1 = 5; int number2 = 10; int total; total = Sum(number1, number2); Console.WriteLine(“Sum is: “ + total); Console.ReadKey(true); }
  • 9. Example 3: A program that will calculate the cube of a number static long cube(long x) { long x_cube; x_cube = x*x*x; return x_cube; } static void Main(string [] args) { long input, answer; Console.Write(“Enter an integer value: “); input = Convert.ToInt64(Console.ReadLine()); answer = cube(input); Console.WriteLine(“The cube of “ + input + “is “ + answer); Console.ReadKey(true); }
  • 10. Example 4: Finding Maximum Value using System; namespace CalculatorApplication { class NumberManipulator { public int FindMax(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } static void Main(string[] args) { /* local variable definition */ int a = 100; int b = 200; int ret; NumberManipulator n = new NumberManipulator(); //calling the FindMax method ret = n.FindMax(a, b); Console.WriteLine("Max value is : {0}", ret ); Console.ReadLine(); } } }
  • 11. Example 5: A method can call itself. This is known as recursion. Recursion is made for solving problems that can be broken down into smaller, repetitive problems. It is especially good for working on things that have many possible branches and are too complex for an iterative approach. Following is an example that calculates factorial for a given number using a recursive function:
  • 12. using System; namespace CalculatorApplication { class NumberManipulator { public int factorial(int num) { /* local variable declaration */ int result; if (num == 1) { return 1; } else { result = factorial(num - 1) * num; return result; } } static void Main(string[] args) { NumberManipulator n = new NumberManipulator(); //calling the factorial method Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6)); Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7)); Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8)); Console.ReadLine(); } } }