Chapter 7 - Functions
Chapter 7 - Functions
PROGRAMMING
Let‘s try this:
Write a program that can add,
subtract, multiply and divide two
numbers.
#include <iostream>
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().
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
Function prototype
Where:
a & b -Parameters
int - datatype
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
{
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>
int main()
{
cout << "The mod is: " << mod(4,5));
}
{
return a % b;
}
#include <iostream>
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)
finval = FuncByValue(finval);
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 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
Example:
void function1(void);
void function2(void);
Example:
void function1(int);
void function2(int,int);
void function3(int,float,int);
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);
Example:
int function1(int);
float function2(int,int);
int function3(int,float,int);
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);
}