BUE Lec5-Functions I
BUE Lec5-Functions I
23COMP02C
• Function Parts.
PROGRAMMING AND
SOFTWARE DESIGN/
• Local vs. Global
23ECE12C
variables.
COMPUTER
• Overloading a PROGRAMMING
function name.
1
Quotes of the Day!
2
1. Top-down Design
• If we look at a big problem as a whole, it may seem
hard to solve because it is so complex.
3
1. Top-down Design – (cont.)
• A methodology of information processing,
problem solving, and knowledge
representation, that starts at the highest
level of a design concept and proceeds
towards the lowest level.
4
1. Top-down Design – (cont.)
Example
• Problem: we want to automate
sending letters for students who
have passed their absence limits.
5
1. Top-down Design – (cont.)
Example
• Possible solution:
1 Get students list from files
2 Sort according to absence times
3 Select those with more than 3 times
4 Print a letter for each student
6
1. Top-down Design – (cont.)
Example
• Problem: Write a program that
draws this picture of a house.
7
1. Top-down Design – (cont.)
Example
• Possible solution:
1. Draw the outline of the house.
2. Draw the chimney.
3. Draw the door.
4. Draw the windows.
Are there any parts
to divide?
8
1. Top-down Design – (cont.)
Example
• Possible solution:
1. Draw the outline of the house
2. Draw the chimney
3. Draw the door
Call Draw Door Frame
Call Draw Knob
9
1. Top-down Design – (cont.)
Example
• Possible solution:
1. Draw the outline of the house
2. Draw the chimney
3. Draw the door
Draw Door Frame
Draw Knob
10
1. Top-down Design – (cont.)
Example
• Possible solution:
1. Draw the outline of the house.
2. Draw the chimney
3. Draw the door
Call Draw Door Frame
Call Draw Knob
4. Draw the windows
Draw a Window in Location 1
Draw a Window in Location 2
Draw a Window in Location 3
Code/get code for each step.
Code reusability
11
1. Top-down Design – (cont.)
• Top-down is a standard way of writing
programs.
• Programs produced using this method and
using the control structures (sequential,
selection and repetition) are called
structured programs.
• Structured programs are easier to test,
modify, and are also easier for
other programmers to understand.
12
1. Top-down Design – (cont.)
Advantages
• Breaking the problem into parts helps us to
clarify what needs to be done.
• At each step of refinement, the new parts
become less complicated and, therefore,
easier to figure out.
• Breaking the problem into parts allows more
than one person to work on the solution.
• Parts of the solution may turn out to be
reusable.
• Typically, each part will be coded as a
separate function.
13
Functions
• I-P-O
– Input – Processing – Output
– Sometimes a function has no input.
– Sometimes a function returns no output.
14
Functions
15
USER-DEFINED FUNCTIONS
16
User-defined Functions
17
2. Programmer-defined Functions
• Three parts in using any function:
1. Function Declaration (Prototype)
• Information for compiler
• To properly interpret calls
3. Function Call
• Using the function
• Transfers control to function body
• Arguments are plugged in for formal parameters
18
1.Declaration
3.Call
2.Definition
19
2. Programmer-defined Functions – (cont.)
(1) Function Declaration (Prototype)
• Syntax:
return_type Fn_name(parameter list);
20
2. Programmer-defined Functions – (cont.)
(1) Function Declaration (Prototype)
…………..
}
21
2. Programmer-defined Functions – (cont.)
(2) Function Definition (Body)
• Syntax:
return_type Fn_name(parameter_list)
{
// your code goes here
}
• Implementation of the function.
• Placed after main() or instead of declaration.
• Returned value type, and formal parameters
type, order and number should match the
function declaration.
22
2. Programmer-defined Functions – (cont.)
(2) Function Definition
24
2. Definition of the function is placed instead
of declaration (before main()).
25
2. Programmer-defined Functions – (cont.)
(3) Function Call
• To execute its body. If it is a void
function(i.e function
Fn_name(arguments_list); returns nothing)
result = Fn_name(arguments_list);
If the function returns A VALUE OF THE
SAME TYPE AS RESULT
• A function to execute must be called in a way or
another in the main function.
• Arguments type, number and order should match
the function declaration.
26
2. Programmer-defined Functions – (cont.)
(3) Function Call
Arguments can be
literals, variables,
expressions, or
combination
27
2. Programmer-defined Functions – (cont.)
28
// declaration
1.void welcome ();
2. void main()
3. {
4. cout << "Main\n";
// call
5. welcome (); // No expression or assignment statement
13. cout << "after calling\n";
14. } // end main
// definition
6. void welcome ()
7. {
8. int repeat;
9. cin >> repeat;
10 for (int i = 1; i <= repeat; i++)
11. cout << "Hi\tWelcome to my game!\n";
12. } // end welcome
29
// declaration Example 2
double sum (double x, double y, double z);
void main()
{
double A, B, C, result;
A = 1; B = 2; C = 3; //cin >> A >> B >> C;
// processing
A++;
// call
result = sum (A, B, C); // call by value
// output
cout << "In main, B: " << B << " and Result: "<< result << endl;
} // end main
// definition
double sum (double A, double B, double C)
{
B = B + 2;
cout << "In function sum, B: " << B << endl;
return A + B + C;
} // end sum
30
2. Programmer-defined Functions – (cont.)
(5) Parameters vs Arguments
• Formal parameters
– In function declaration
– In function definition’s header
Calling by value
32
2. Programmer-defined Functions – (cont.)
Function call (by value)
arguments_list
parameters_list
33
2. Programmer-defined Functions – (cont.)
Keep in mind: Calling/Passing by value
1. The value of the argument (not the
variable) that is plugged in for the formal
parameter.
2. Arguments are plugged in for formal
parameters in the order they appear in the
function call.
3. When an argument is plugged in for a
formal parameter, it is plugged in for ALL
instances of this parameter that occur in the
function body.
34
35
2. Programmer-defined Functions – (cont.)
(6) Local Variables
– Declared inside body of given function
– Available only within that function
• Can have variables with same names
declared in different functions
– Scope is local: "that function is its scope"
• Local variables preferred
– Functions should declare whatever local
data needed to “do their job”.
36
2. Programmer-defined Functions – (cont.)
Local Variables
• A function can
declare its own local
variables as needed.
37
2. Programmer-defined Functions – (cont.)
Local Variables
• Can have variables
with same names
declared in
different functions
• Same name BUT
they are totally
different locations
in memory.
38
Exercise
39
#include <iostream>
using namespace std;
void main()
{
int n1, n2, n3;
cout << " Enter three numbers\n";
cin >> n1 >> n2 >> n3;
if (check)
cout << "In order\n"; If(isInOrder( n1, n2, n3))
else
cout << "Not arranged\n";
Salary Tax
2000 - 3999 5%
Between 4000 and 6000 7%
More than 6000 10%
41
#include <iostream>
using namespace std;
void main()
{
float empSal;
cout << "Enter the salary\n";
cin >> empSal;
if (tax != -1)
cout << tax << endl;
else
cout << "Invalid salary\n";
} 42
Salary Tax
2000 - 3999 5%
Between 4000 and 6000 7%
More than 6000 10%
float CalcTax (float sal)
{
if (sal >= 2000 && sal < 4000)
return sal * 0.05;
else if (sal >= 4000 && sal <= 6000)
return sal * 0.07;
else if (sal > 6000)
return sal * 0.1;
else
return -1; // there is a problem
}
43
2. Programmer-defined Functions – (cont.)
(7) Global Constants/Variables
– Declared outside all functions, hence
defined for all functions below it.
44
2. Programmer-defined Functions – (cont.)
45
Constant is global to
all functions below
its declaration
46
Constant global to
all functions below
its declaration
What if we declared a
local variable with the
same name as a global
one?
47
48
2. Programmer-defined Functions – (cont.)
(8) Overloading a Function Name
• Having more than one function
with the same name
• How does the compiler know?
– By number of parameters
– By type of parameters
– NOT by return value
• Should be used with care!
49
2. Programmer-defined Functions – (cont.)
(8) Overloading a Function Name
How does the
compiler know?
By number of parameters
50
double avg(double, double); How does the
double avg(double, double, double); compiler know?
double avg(int, int);
By type of parameters
int main() or number of parameters
{
double A=1, B=2, C=3;
int X=1, Y=2, Z=3;
cout<<"Integer avg="<< avg (X,Y) << endl;
cout<<"Double avg= "<< avg (A,B) << endl;
cout<<"Double avg= "<< avg (A,B,C) << endl;
return 0;
}
double avg (double n1, double n2, double n3)
{ return (n1+n2+n3)/3.0; }
double avg (int n1, int n2) What if avg was called with
{ return (n1+n2)/2.0; } two different arguments type?
Try it at home and tell me
double avg (double n1, double n2) next lecture ☺
{ return (n1+n2)/2.0; }
51
Default Parameter Values
• When you define a function, you can
specify a default value for each of the last
parameters. This value will be used if the
corresponding argument is left blank when
calling to the function.