Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Functions (Part1) Spring2022mod

The document discusses modularity in programming through the use of functions and modules. It explains that programs should be broken down into subprograms or modules to make them more manageable and readable. Each module should perform a single well-defined task. The concepts of formal and actual parameters are introduced, as well as how functions can return values. Modular programming helps make programs more reliable, maintainable and easier to debug.

Uploaded by

Mark Ojo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Functions (Part1) Spring2022mod

The document discusses modularity in programming through the use of functions and modules. It explains that programs should be broken down into subprograms or modules to make them more manageable and readable. Each module should perform a single well-defined task. The concepts of formal and actual parameters are introduced, as well as how functions can return values. Modular programming helps make programs more reliable, maintainable and easier to debug.

Uploaded by

Mark Ojo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Introduction to Modularity

• Function/procedures
• void/value-returning
• Arguments or parameters
• Formal parameters vs. actual parameters
• Pass by value/ Pass by reference
• Scope: global/local/name precedence

cosc175/module.ppt 1
Modularity
• Most programs so far have been simple -
less than one page in length
• In reality, most problems are not so simple
• top-down design - identify first the major
tasks and further subtasks within them
• Modularity – breaking a program into
subprograms

cosc175/module.ppt 2
Module
• section of an algorithm which is dedicated
to a single function
1. performs one single function
2. single entry, single exit
3. short enough to be easily read and
modified
4. long enough to perform function
cosc175/module.ppt 3
Modules
• all tasks can be subdivided and further
subdivided into subtasks
• goal => can easily construct pseudocode for
each subtask
• each module constructed and tested as unit
• Black Box
• Suggestion: Stubbing in (write dummy
modules)
– Always have something working
cosc175/module.ppt 4
good names help
• Begin with Verb!
• Use several short words
– PrintPageHeadings
– CalcSalesTax
– ValidateInputDate

cosc175/module.ppt 5
Mainline or driver
• call subtasks
• should show the main processing functions,
the order they should be performed
• easy to read
• manageable length
• Should include a loop!

cosc175/module.ppt 6
Calling a subprogram/module
int main() • line2 Invokes or calls the
{ subprogram
… //line1 • Control is transferred to the
DoProc1(); //line2 subprogram: line5
….//line3 • The code in the subprogram is
executed: line 6,7,8
} // end main //line4
• At line8, control returns to
//*************************** statement following call, line 3
void DoProc1() • 1,2,5,6,7,8,3,4
{ //line5
….. //line6
….. //line7
} // end DoProc1 //line8

cosc175/module.ppt 7
Hierarchy Chart
shows who calls whom
• illustrates structure of the solution to a problem
– i.e organization chart for a company
• shows top-down design, communication flow
• One block for each module
• Program name in first block, begin with verb

cosc175/module.ppt 8
int main()
{ .
.
DemoC();
DemoB();
DemoA();
return 0;
}
//***************
void DemoA()
DoStuff
{
. DemoA DemoB DemoC
.
}
//****************
void DemoB()
{
.
.
}
//****************
void DemoC()
{
.
. cosc175/module.ppt 9
}
Writing larger, more complex Programs
1. Define the problem
– Input/Output
– Processing => list of activities to be performed
2. Plan
1. Modularize => Hierarchy chart
• Group list of activities into modules
2. Construct code for main-line:
Initial processing
Loop
processing //Contains calls to major processing modules(stubs)
END Loop
Final Processing
3. Construct code for each successive module in hierarchy chart
4. Desk-check each module in top-down fashion

cosc175/module.ppt 10
advantages of well structured
programs:
– can easily be changed, updated and maintained
– division into tasks makes them easy to
understand
– easy to construct
– can test modules individually
– easy to test and debug - easier to isolate errors
• can have each module print input and output
– more reliable - fewer bugs
cosc175/module.ppt 11
void function Value-returning
function
return Can return one or returns one result
more values
GetVals(x,y,z) return x * x
name Begins with a verb Noun
CalcSquare(..) Square(..)
  void function Value returning function
void CalcSquare(..) float Square(..)
call stand-alone Call is part of expression
CalcSquare(..) x = Square(..)
Multipurpose Usually mathematical
cosc175/module.ppt 12
cosc175/module.ppt 13
14
15
16
17
18
void function example
// prints lines of *’s where numLines specifies
// how many lines to print
void PrintLines( int numLines )
{
int count;
  for (count = 1; count <=numlines; count++)
cout << "***************" << endl;
}

cosc175/module.ppt 19
function as part of a program
int main()
{
PrintLines(2);
cout << " Welcome Home!“ << endl;
PrintLines(2);
return 0;
}
//***********************************************
// prints lines of *’s
// numLines specifies how many lines to print
void PrintLines( int numLines )
{
int count;
  for (count = 1; count <=numlines; count++)
cout << "***************" << endl;
} //End PrintLines

cosc175/module.ppt 20
Value returning Function examples
//******************************
//this function returns the cube of x
int Cube (int x)
{
return x * x * x;
}
//************************************************
  //this function returns the maximum of 2 numbers
int Max (int num1,int num2)
{
int max;
if (num1 > num2 )
max = num1;
else
max = num2;
return max;
}
cosc175/module.ppt 21
22
Output

Option1: Value Returning Function (Call using assignment statement)

23
24
Output

Option2: Value Returning Function (Call using cout statement)

25
Functions in a program
void ShowFuncs()
{
int num1,num2;
cout << “Enter two numbers”
cin >> num1 >> num2;
cout << "The max of " << num1 << " and << " num2 " is " << Max(num1,num2);
cube = Cube(num1)
cout << "The cube of " << num1 << " is " << Cube(num1);
}
//**********************************************
//this function returns the cube of x
int  Cube (int x)
{
return x * x * x;
}
//************************************************
 //this function returns the maximum of 2 numbers
int Max (int num1,int num2)
{
int max;
if (num1 > num2 )
max = num1;
else
max = num2;
return max;
}

cosc175/module.ppt 26
When to use void versus value-
returning Functions
• When returning more than one value - use
void
• when I/O is required - use void
• returning one Boolean value – value-
returning
• returning one value to be used immediately
in an expression – value-returning
• when in doubt - use void
cosc175/module.ppt 27
arguments or parameters
• In function definition (formal arguments):
void Name(type arg1, type arg2,…type argn)
• In call to function (actual arguments)
Name(arg1,arg2,…argn)
• Arguments must match in number, order
and data type but not name
• Can be 0,1, or many arguments

cosc175/module.ppt 28
Formal and Actual Arguments
• Formal Argument
• In the definition
– void PrintLines(int lines)
• Actual Argument
• In the call
• PrintLines(2); // could be constant:
• PrintLines(num); //could be variable:
• PrintLines(num+2) //could be Expression
• Note: actual parameter and formal parameters may
have different names
cosc175/module.ppt 29
Multiple parameters:
• matched by position
• each param must be declared:

for example:
PrintLines(3,’$’); // call a void finction

void PrintLines(int numLines,char whichChar)


{
…..
}

cosc175/module.ppt 30

You might also like