ch4 5
ch4 5
Programming
Fundamentals
DR. RAHEEL AHMED MEMON
2
Functions
3
What is a function?
u Library functions
u These are the in-built functions of ‘C++ ’ library.
u These are already defined in header files.
u Example: sizeof() is a function which is used to
know the size of any datatype. It is defined in
‘climit’ file .
Pseudocode
and
Flowchart
10
Benefits of Top-Down
Design
u Subtasks, or functions in C++, make programs
u Easier to understand
u Easier to change
u Easier to write
u Easier to test
u Easier to debug
u Easier for teams to develop
11
Program Testing
u Programs that compile and run can still
produce errors
u Testing increases confidence that the program
works correctly
u Run the program with data that has known output
u You may have determined this output with pencil
and paper or a calculator
u Run the program on several different sets of data
u Your first set of data may produce correct results in
spite of a logical error in the code
u Remember the integer division problem? If
there is no fractional remainder, integer
division will give apparently correct results
12
Predefined
Functions
13
Predefined Functions
u Function_name (Argument_List)
u Argument_List is a comma separated list:
u Example:
u side = sqrt(area);
u cout << “2.5 to the power 3.0 is “ << pow(2.5,
3.0);
Function Libraries 16
#include <cmath>
u Newer standard libraries, such as cmath, also require
the directive
using namespace std;
Other Predefined Functions 17
int main() {
double num;
return 0;
}
Random Number 19
Generation
srand(time(0));
Practice
with
library
functions
Programmer-
Defined Functions
Programmer-Defined Functions 24
Declaration
u Tells the return type
u Tells the name of the function
u Tells how many arguments are needed
u Tells the types of the arguments
u Tells the formal parameter names
u Formal parameters are like placeholders for the
actual
arguments used when the function is called
u Formal parameter names can be any valid identifier
u Example:
function header
u Example:
Function:
u 4 Stages
u *Function Declaration // int myfunc(int a);
u Function Definition // int myfunc(int a){ a=a+5; return a; }
u Function call // myfunc(5);
u Function Return Value // 10
33
Procedural
Abstraction
Procedural 34
Abstraction
u The Black Box Analogy
u A black box refers to something that we know
how
to use, but the method of operation is unknown
u A person using a program does not need to know
how it is coded
u A person using a program needs to know what
the
program does, not how it does it
u Functions and the Black Box Analogy
u A programmer who uses a function needs to
know
what the function does, not how it does it
Functions
u Write functions so the declaration and comment
is all a programmer needs to use the function
u Function comment should tell all conditions
required of arguments to the function
u Function comment should describe the returned
value
u Variables used in the function, other than the
formal parameters, should be declared in the
function body
39
Formal Parameter
Names
u Functions are designed as self-contained
modules
u Different programmers may write each function
u Programmers choose meaningful names for
formal parameters
u Formal parameter names may or may not
match
variable names used in the main part of the
program
u It does not matter if formal parameter names
match other variable names in the program
u Remember that only the value of the
argument is
plugged into the formal parameter
40
Go through the
book for
Case study of:
Buying PIZZA
41
Local Variables
Local Variables 42
Function
Overloading
Overloading 53
Function Names
u C++ allows more than one
definition for the
same function name
u Very convenient for situations in
which the “same”
function is needed for different
numbers or types
of arguments
u Overloading a function name
means providing
more than one declaration and
definition using
the same function name
Function Overloading in C++ 54
u Overloaded functions
u Must have different numbers of formal
parameters
AND / OR
u Must have at least one different type of
parameter
Functions Add-ons..
void Functions
void-Functions
Call by Reference
Call by Value 65
Call by Value 66
Call by Value 67
Call-by-Reference Parameters
Call by
Ref. vs.
Call by
value
Mixed Parameter Lists
u Example:
void goodStuff(int& par1, int par2, double& par3);
u par1 and par3 are call-by-reference formal
parameters
u Changes in par1 and par3 change the argument variable
u par2 is a call-by-value formal parameter
u Changes in par2 do not change the argument variable
72
Recursion
What is recursion?
Recursion
IN ALL PATTERNS
75
Recursion
IN NATURE
Recursion
int f(int x)
{
int y;
if(x==0)
return 1;
else {
y = 2 * f(x-1);
return y+1;
}
}
Example: 77
#include <iostream>
using namespace std;
int sum(int n) {
if (n == 1) {
return 1;
} else {
return n + sum(n - 1); // Recursion of sum, keep on repeating until its !=1.
}
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
int result = sum(num);
cout << "Sum of numbers from 1 to " << num << " is: " << result << endl;
return 0;
}
78