Unit-1-User-Defined-Functions IN COMPUTER PROGRAMMING
Unit-1-User-Defined-Functions IN COMPUTER PROGRAMMING
Unit-1-User-Defined-Functions IN COMPUTER PROGRAMMING
Unit 1
User-Defined Functions
Introduction
Learning Objectives
1
Unit 1: User-Defined Functions
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
2
Unit 1: User-Defined Functions
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 1st 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
3
Unit 1: User-Defined Functions
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.
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.
using System;
namespace FunctionExample
{
class Program
{
// User defined function without return type
public void Show() // No Parameter
{
Console.WriteLine("This is non parameterized function");
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
Program program = new Program(); // Creating Object
program.Show(); // Calling Function
}
}
}
4
Unit 1: User-Defined Functions
When the above code is compiled and executed, it produces the following
result:
When the above code is compiled and executed, it produces the following
result:
Hello Philippines
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.
5
Unit 1: User-Defined Functions
Parameters are specified within the pair of parentheses in the function
definition, separated by commas. When we call the function, we supply the
values in the same way. Note the terminology used - the names given in the
function definition are called parameters whereas the values you supply in the
function call are called arguments.
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
6
Unit 1: User-Defined Functions
Console.ReadKey(true);
}
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();
}
}
}
7
Unit 1: User-Defined Functions
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;
}
}
8
Unit 1: User-Defined Functions
When the above code is compiled and executed, it produces the following
result:
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.
9
Unit 1: User-Defined Functions
Output:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 50
C# Call By Reference
10
Unit 1: User-Defined Functions
Output:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 2500
C# Out Parameter
11
Unit 1: User-Defined Functions
Output:
The following example demonstrates that how a function can return multiple
values.
12
Unit 1: User-Defined Functions
Output:
Value before passing
val1 = 50
val2 = 100
Value after passing
val1 = 25
val2 = 25
Reflection
As a BSIT/BSCS student, what do you think is the importance of learning how
to develop programs with the use of user-defined functions? Do you think it
gives you a lot of advantage most especially in solving complex problems?
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
_________________________________________________________
https://www.thoughtco.com/introduction-to-functions
https://www.learncpp.com/cpp-tutorial/function-return-values/
https://www.w3resource.com/
https://liucs.net/cs102f15/programming-rubric.pdf
https://openclassrooms.com/en/courses/5670356-learn-programming-with-c
13