You are on page 1of 22

UNIT 1 -

PROGRAMMING
LECTURE 6 – PROCEDURAL PROGRAMMING
TOPICS

 Functions
 Function definition
 Function Call
 Argument and return
 Passing argument

Unit 1 - Programming / Lecture 5 - Loop statements 2


NEED FOR FUNCTIONS

Real world applications become more complex and large, several


problems arise
o Algorithm are more complex, hence more difficult to design
o Implementation of complex algorithm is difficult because of the size of the
program
o Larger program, testing, debugging, and maintenance will be difficult
SOLUTION

 Complex problems can be solved by breaking them into a set of sub-


problems (modules)
o Each is implemented independently
o Then can be combined to a single unit
 C# supports modularity by means of functions (or methods)
ADVANTAGES OF FUNCTIONS

 Facilitates top-down modular programming


o Modularity brings logical clarity to the programs
 Avoids the need for redundant code
o Repeated instructions can be written as a function
o Which can then be called whenever it is needed
 Facilitates reusability
o Created in one program can be used in others
FUNCTION PROTOTYPES

 Like variables, functions must be declared before it’s called


o Declaration of a function is called Function Prototype or signature (name) of the
function
 Function prototype specifies
o The signature (name) of the function
o The return type
o Number and data types of the parameters
FUNCTION PROTOTYPE: EXAMPLES
/*function findBig
* - returns integer value (larger one),
* - takes 2 integer arguments*/
int findBig(int x, int y)

/* function 'swap'
* - does not return any value
* - takes 2 pointer variables*/
void swap(int *, int *)

/* function add
* - returns float value
* - takes 1 float variable and 1 integer variable*/
float add(float x, int y)
FUNCTION DEFINITION

 Function definition is used to define the function with


o Appropriate name, parameters, return type and
o Operations to be carried out by the function
 Function definition has two main components
o Function header (first line, function prototyping)
o Function body (operations within the curly brackets)
EXAMPLE: FUNCTION DEFINITION
static void Main(string[] args)
{
int a, b;
Console.Write("Please input a: ")
a = Int32.Parse(Console.ReadLine());
Console.Write ("Please input b: ")
b = Int32.Parse(Console.ReadLine());
int result = sum(a, b);
Console.WriteLine("Result {0}", result);
}
/*function definition*/
static int sum(int x, int y)
{
return x + y;
}
FUNCTION DEFINITION: GENERAL FORM

return-type function-name(type arg1, type arg2, …)


{
local variables declaration;
executable statement 1;
executable statement 2;
.
.
.
return expression
}
FUNCTION HEADER

Function header includes of


o function-name, specifies the name of the function and it must be a valid
identifier
o arg1, arg2, …, specifies formal arguments (parameters)
o return-type, specifies the data type of the data item to be returned from the
function
FUNCTION BODY

Function body can have


o Any declaration statements (for local variables)
o Any valid executable statements
o Return statement (if applicable)
LOCAL/GLOBAL VARIABLES

Local variables are declared inside a function, they are local to that function
o It can be accessed only within that function
o Memory for it is allocated only when the function is invoked
o Memory for it is de-allocated when the control moves out of the function
Global variables are declared outside of the functions, they are common to all
functions
o It can be accessed by all functions in the program
o Memory for it is allocated when “program” gets executed
o Memory is de-allocated only at the end of the “program” execution
EXAMPLE: LOCAL/GLOBAL VARIABLES
RETURN STATEMENT

It’s used to transfer the control back to the calling program


Function may or may not return a value
o If it returns a value, it is achieved by the return statement
o There could be multiple return statements
General form
o return;
o return (expression);
EXAMPLE: RETURN STATEMENT

return; //doesn't return any value (exit function call)


return 0; //returns zero
return (a*b);//returns the product of a & b
return (a<b);//returns True(1) or False (0)
FUNCTION CALL

Functions are invoked by


o Specifying its name,
o Followed by a list of arguments within parentheses
General form
o [variable name =] function-name(actual-argument)
When the function is encountered, the control is transferred to the called function
o Means the statement(s) of the function is executed
LHS variable is optional
o It’s used to store returning values from the function
EXAMPLE: FIND LARGER INTEGER
static void Main(string[] args)
{
int a, b;
Console.Write("Please input a: ")
a = Int32.Parse(Console.ReadLine());
Console.Write ("Please input b: ")
b = Int32.Parse(Console.ReadLine());
int result = sum(a, b);
Console.WriteLine("Result {0}", result);
}
/*function definition*/
static int sum(int x, int y)
{
return x + y;
}
ACTIVITY

 Rewrite some of previous program as functions then call them in Main


o Find max
o Print numbers from 1 to n
o Sum numbers from 1 to n
o Enter n, print an asterisk triangle of n rows
PASSING ARGUMENTS

 The values of the actual arguments are copied into the respective
formal arguments
 Actual and formal arguments
o Refer to the different memory locations and
o Value of actual argument is copied into the formal argument
 So, any changes made to the formal argument are not reflected in their
corresponding actual arguments
CALL/PASS BY VALUE EXAMPLE
ACTIVITY: DESIGN FUNCTIONS FOR A MENU
BASED PROBLEM
 Given a menu based program with 4 options
 Draw a Product Breakdown Structure
 Identify modules (functions) with inputs / outputs
 Implement modules
 Implement main function

Unit 1 - Programming / Lecture 5 - Loop statements 22

You might also like