Professional Documents
Culture Documents
User-Defined Functions
Introduction
In C#, a function is a way of packaging code that does something and then
returns the value. Unlike in C, C++ and some other languages, functions do
not exist by themselves. They are part of an object-oriented approach to
programming.A program to manage spreadsheets might include a sum()
function as part of an object, for example.
There are two types of function. Predefined or built-in functions are those that
are part of the C# library. These are present in any of the various headers that
can be included in a program. If your need these functions, just call them
anywhere in your main method or inside other methods or user-defined
functions. On the other hand, User-defined functions are programmed
routines that have its parameters set by the user of the system. User- defined
functions often are seen as programming shortcuts as they define functions
that perform specific tasks within a larger system.
In this unit, we will focus on discussing user-defined functions. You will learn
how to create, define, and call this type of function in our C# programs.
Learning Objectives
Presentation of Contents
What is a Function?
The 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:
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.
Many functions use arguments, which are values passed to the function when
it is called. A function needs to know what kind of arguments to expect – the
data type of each argument. You can pass a function any of C#’s data types.
Argument type information is provided in the function header by the parameter
list. To pass arguments to a function, you list them in parenthesis following the
function name. The number of arguments and the type of each argument
must match the parameters in the function header. For example, if a function
is defined to take two type int arguments, you must pass it exactly to int
arguments – no more, no less – and no other type. If the function takes
multiple arguments, the arguments listed in the function call are assigned to
the function parameters in order: the 1stargument to the 1 st parameter, the 2nd
argument to the 2nd parameter, and so on.
You may be wondering where in your source code you should place your
function definitions. The basic structure of a program that uses functions is
shown below:
A C# program does not execute the statements in a function until the function
is called by another part of the program. When a function is called, the
program can send information in the form of one or more arguments. An
argument is program data needed by the function to perform its task. The
statements then in a function execute, performing whatever task each was
designed to do. When the function’s statements have finished, execution
passes back to the same location in the program that called the function.
Functions can send information back to the program in the form of a return
value.
To return a value from a function, you use the keyword return, followed by a
C# expression. When execution reaches a return statement, the expression is
evaluated, and execution passes the value back to the calling program. The
return value of the function is the value of the expression.
When the above code is compiled and executed, it produces the following
result:
You can also add parameters in a function and a function can also return
values. A parameter is the symbolic name for "data" that goes into a function.
A function can take parameters which are just values you supply to the
function so that the function can do something utilizing those values. These
parameters are just like variables except that the values of these variables are
defined when we call the function and are not assigned values within the
function itself.
The specific value returned from a function is called the return value. When
the return statement is executed, the return value is copied from the function
back to the caller.
When the above code is compiled and executed, it produces the following
result:
Sum is: 15
When the above code is compiled and executed, it produces the following
result:
Enter an integer value: 5
The cube of 5 is 125
using System;
namespace CalculatorApplication
{
class NumberManipulator
{
public int FindMax(int num1, int num2)
{
/* local variable declaration */
int result;
int ret;
NumberManipulator n = new NumberManipulator();
//calling the FindMax method
ret = n.FindMax(a, b);
Console.WriteLine("Max value is : {0}", ret );
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following
result:
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;
}
}
When the above code is compiled and executed, it produces the following
result: