PRINCIPLE OF
PROGRAMMING
Let‘s try this:
Write a program that can add,
subtract, multiply and divide two
numbers.
#include <iostream>
using namespace std;
int main ()
{
int a, b, addresult, subresult, mulresult, divresult;
cout << "Enter two integers : ";
cin >> a >> b;
addresult = a+b;
subresult = a-b;
mulresult = a * b;
divresult = a / b;
cout << endl << endl << "Addition result = " << addresult;
cout << endl << "Subtract result = " << subresult;
cout << endl << "Multiply result = " << mulresult;
cout << endl << "Divide result = " << divresult << endl;
system("pause");
}
There are so many
functions in main().
We can separate the functions
(add, subtract, multiply and
divide ) into several
functions.
So, how to
divide them ?
#include <iostream>
using namespace std;
int main()
{
int a,b, tolak, mulresult, addresult, subresult;
int divresult;
//Input value
//Input value
cout << "Enter two integers : ";
cout << "Enter two integers : ";
cin >> a >> b; cin >> a >> b;
//Arithmetic operation
addresult = a + b; //Arithmetic operation
subresult = a - b; addresult = a + b;
mulresult = a * b; subresult = a - b;
divresult = a / b; mulresult = a * b;
divresult = a / b;
//Output printing
cout << "Addition result = " << addresult << endl;
//Output printing
cout << "Subtract result = " << subresult << endl;
cout << "Addition result = " << addresult << endl;
cout << "Multiply result = " << mulresult << endl; cout << "Subtract result = " << subresult << endl;
cout << "Divide result = " << divresult << endl; cout << "Multiply result = " << mulresult << endl;
cout << "Divide result = " << divresult << endl;
}
Modular programming in C++ is called Functions
Breaking a program up into smaller, manageable functions or
modules
Module can be divided into sub-modules
Benefits:
Program structure is readable
Make program development more manageable
Problem can viewed in a smaller scope
Program development are much faster compared to the common
structure
Software reusability – using existing functions as building block to
create new program
Improves maintainability of programs
Simplifies the process of writing programs
Function Is a program segment that performs specific
tasks, made up of C++ statements
Has TWO types:
User-defined functions
Created by user for
Standard library functions specific purposes
-Example:
exit(), cin, cout, getch()
Function prototype
Functions Function definition
elements
Function call
#include <iostream>
using namespace std; //Arithmetic operation
void Calculate()
int a, b, mulresult, addresult, subresult; {
int divresult; addresult = a + b;
void InputValue(); subresult = a - b;
void Calculate(); Function mulresult = a * b; Function
void PrintResult(); prototype divresult = a / b; definition
}
int main() //Output printing
{ void PrintResult()
InputValue(); {
Calculate(); Function call cout << "Addition result = " << addresult << endl;
PrintResult(); cout << "Subtract result = " << subresult << endl;
} cout << "Multiply result = " << mulresult << endl;
cout << "Divide result = " << divresult << endl;
//Input value }
void InputValue()
{
cout << "Enter two integers : ";
cin >> a >> b;
}
Must be added to a C++ program before the main
function, IF call that function before defining it.
It tells the compiler:
What type of value the function returns
Number and types of parameters
Order in which these parameters are expected
There is no need for a prototype if the function is
called after its definition
.
void addition (int a, int b);
or
void addition (int, int);
Where:
a & b -Parameters
int - datatype
addition - Function name
void - Function return type
#include <iostream>
using namespace std;
void addition ( int, int ); Function Prototype
void subtract ( int, int );
int main ( )
{
int a, b;
cout >> a >> b;
addition(a,b);
subtract(a,b);
}
void addition (int a, int b)
{
int addresult = a+b;
cout << “Addition result = ” << addresult;
}
void subtract (int a, int b)
{
int ubresult = a-b;
cout << “Subtract result = ” << subresult;
}
A function definition has two principal components:
The first line (including argument declarations)
The body of the function.
The first line of a function definition contains
Type of the value returned by the function (the function
type)
The function name
An optional list of formal parameters, separated by
commas and enclosed in parentheses
#include <iostream>
using namespace std;
void addition ( int, int );
void subtract ( int, int );
int main ()
{
int a, b;
cout >> a >> b; Formal parameters
addition(a,b); data type
subtract(a,b);
}
a and b are
Function Function formal
return type name parameters
void addition (int a, int b)
{ Function
addresult = a+b; Definition
cout << “Addition result =” << addresult;
}
void subtract (int a, int b)
{
subresult = a-b;
cout << “Subtract result = ” << subresult);
}
void addition (int a, int b) First line
{
addresult = a+b;
cout << “Addition result =” << addresult;
}
Body
Format a function definition:
functionreturn_type function_name (formal_parameters)
{
declaration
statement
} A return value of type void
indicates a function
does not return a value.
void addition (int a, int b)
{
addresult = a+b;
cout << “Addition result =” << addresult);
}
a and b are
Parameters/
Arguments
void addtion (int a, int b)
{
addresult = a+b;
cout << “Addition result = ” << addresult;
}
#include <iostream>
using namespace std;
int mod(int, int); /* Function Prototype */
int main()
{
cout << "The mod is: " << mod(4,5));
}
int mod(int a, int b) /* Function Definition */
{
return a % b;
}
#include <iostream>
using namespace std;
int sum(int a, int b) /* Function Definition */
{
return a+b;
}
int main()
{
printf("The sum is: " << sum(4,5));
}
A function can can be accessed (i.e., called) by
specifying function name, followed by a list of arguments enclosed
in parentheses and separated by commas
If the function call does not require any arguments
empty pair of parentheses must follow the name of the function.
The arguments appearing in the function call are referred to as
actual arguments, in contrast to the formal arguments that
appear in the first line of the function definition.
#include <iostream>
void addition ( int, int );
void subtract ( int, int );
int main ()
{
int a, b;
cin >> a >> b;
addition(a,b); a and b are actual
subtract(a,b); Function calls parameters /
} arguments in
function call
void addition (int c, int d)
{
Function definition
addresult = c + d;
cout << “Addition result =” << addresult);
}
c, d, e and f are
void subtract (int e, int f) formal parameters /
{ arguments in
subresult = e - f; function definition
cout << “Subtract result = ” << subresult);
}
In a normal function call
there will be one actual argument for each formal argument.
may be expressed as constants, single variables, or more
complex expressions.
If the function returns a value, the function access is
written a statement; e.g.,
b = sum(a);
If the function does not return anything, the function
access appears by itself; e.g.,
view(a,b,c);
A function can be called by
the main function
other functions
itself (recursion)
In C++, functions call can be
by value
by reference
• Example Call By Value
finval = FuncByValue(finval);
/*The FuncByValue function*/
finval is type
float float FuncByValue(float fval)
to receive the
result of {
fval*fval return fval*fval;
}
• Example Call By Reference
FuncByReference(&finref)
• Use & to pass the address of a variable to the function
• Value of the referenced variable passed to the function is
modified after returning from the function.
the function void FuncByReference(float *fvalptr)
access appears {
by itself since it *fvalptr = *fvalptr * *fvalptr;
does not return }
a value
•Scope determine where the variable will be used.
•Two types of scope: GLOBAL and LOCAL
It’s scope begins at the point of its
GLOBAL variable declarations and terminates at the end
of file.
Example
#include <iostream>
Declared before main() using namespace std;
int a,b;
a, b and c are float c;
global variable int main()
{
}
Variable that can be accessed inside the
main() function or the function that
LOCAL variable declares it.
Example
#include <iostream>
Declares inside
using namespace std;
main() function or any
void getvalue();
other functions
int main()
{ int x; }
x is a local variable to
main() function and z is a void getvalue()
local variable to getvalue() { int z;
…………
function
}
Example 1:
#include <iostream>
using namespace std;
int a, b; //global variable
void display();
int main()
{ cout << “\nEnter value of a and b : ”;
cin >> a >> b;
display();
}
void display()
{
cout << “Value a is ” << a << “and b is ” << b << endl;
}
Example 2:
#include <iostream>
using namespace std;
Output?
int a, b; //global variables
void display();
int main()
{ int c; //local variable
a= 5;
b = 9;
c = 12;
display();
cout << “\nValue c is ” << c << endl;
}
void display()
{
cout << “\nValue a is ” << a << endl;
cout << “\nValue b is ” << b << endl;
cout << “\nValue c is ” << c << endl;
}
Example 3:
#include <iostream> Output?
using namespace std;
void display();
void main()
{ int c; // local variable
a= 5;
b = 9;
c = 12;
display();
cout << “\nValue c is ” << c << endl
}
void display()
{ int a, b; //local variables
cout << “\nValue a is ” << a << endl;
cout << “\nValue b is ” << b << endl;
}
Example 4:
#include <iostream>
using namespace std; void function1()
{ int i,j ;
void function1(); i= 3;
void function2(); j = i;
int z; cout << “\nValue i is ” << i << endl;
cout << “\nValue j is ” << j << endl;
int main() }
{ function1();
x = 7; void function2()
z = 15; { int x = 4;
function2(); z = j;
} cout << “\nValue x is ” << x << endl;
cout << “\nValue z is ” << z << endl;
z is a global variable, so it
}
can be used by any function
including main()
1. Does not receive and not return value
2. Receive but does not return any value
3. Receive and return value
Format
function type function_name (parameter_list);
Determine whether Determine whether
function returns a value functions receive a value
1.Function that does not receive and not return value
Example:
void function1(void);
not return value not receive value
void function2(void);
not return value not receive value
Example:
Prototype: #include <iostream>
void input(); using namespace std;
Call: void input(); //function prototype
input();
int main()
Definition: {
input(); //function call
void input() }
{ cout << “Enter input :”;
cin >>input; //function definition
} void input()
{ cout << “Enter input :”;
cin >>input;
}
2. Function that receive but does not return any value
Example:
void function1(int);
not return value receives integer value
void function2(int,int);
not return value receives two integer values
void function3(int,float,int);
not return value receives two integers and a float values
Example 1:
Prototype: #include <iostream>
void function1(int); using namespace std;
Call: void function1(int);
function1(a);
int main()
Definition: { int a ;
cout << “Enter a value:”;
void function1(int a) cin >> a;
{ cout << “Value a is ” << a; } function1(a);
}
void function1(int a)
{ cout << “Value a is ” << a; }
Example 2:
Prototype: #include <iostream>
using namespace std;
void print_discount(int); void print_discount(int);
Call: int main()
print_discount(quantity); { int quantity;
cout << “Enter quantity : ”;
Definition: cin >> quantity;
void print_discount(int b) print_discount(quantity);
{ if (b > 1000) }
printf(“Discount = 50%”); void print_discount(int b)
else { if (b > 1000)
printf(“Discount = 10%”); cout << “Discount = 50%”;
} else
cout << “Discount = 10%”;
}
3. Function that receives and returns value
Example:
int function1(int);
returns value receives integer value
float function2(int,int);
returns value receives two integer values
int function3(int,float,int);
returns value receives two integers and a float values
Prototype: #include <iostream>
float calculate_discount(int); using namespace std;
float calculate_discount(int);
Call:
void main()
calculate_discount(kuantiti);
{ int quantity;
cout << “Enter quantity :”;
Definition: cin >> quantity;
cout << “Discount” << calculate_discount(quantity);
float calculate_discount(int b) }
{ if (b > 1000)
discount = 0.5; float calculate_discount(int quantity)
else {
float discount;
discount = 0.1;
if (quantity > 1000)
return discount; discount = 0.5;
} else
discount = 0.1;
return discount;
}
#include<iostream>
using namespace std;
void change1(int);
int change2(int);
int main()
{
int a=5;
int b;
cout << "Before change1: a = " << a << endl;
change1(a);
cout << "After change1: a = " << a << endl;
b=change2(a);
cout << "After change2: a = " << a << " b = " << b << endl;
system("pause");
}
void change1(int a)
{
a=2*a;
cout << "Value of a in change1 function: " << a << endl;
}
int change2(int b)
{
int a;
a=4;
b=a + b;
return (b);
}
Give the function prototype for each of the following:
Function smallest that takes three integers, x, y,
z and returns an integer.
Function instructions that does not receive any
arguments and does not return a value.
Function inToFloat that takes an integer argument,
number, and returns a floating-point result.
#include<stdio.h>
#include <conio.h>
void change1(int);
int change2(int);
int main()
{
int a=5;
int b;
printf("Before change1: a=%d\n",a);
change1(a);
printf("After change1: a=%d\n",a);
b=change2(a);
printf("After change2: a=%d b=%d\n",a,b);
getch();
}
void change1(int a)
{
a=2*a;
printf("Value of a in change1 function: %d\n",a);
}
int change2(int b)
{
int a;
a=4;
b=a + b;
return (b);
}