Академический Документы
Профессиональный Документы
Культура Документы
A function is a self contained body of code to perform a specific task. For instance, a function to sort a list
of integers.
Features:
- Function helps organizing code based on specific purpose. Increase modularity of the code to a
certain extent.
- Help implementing code that has recurring utilization in the program.
- We can pass arguments to a functions. Different type and number of arguments can be passed to a
function.
- A function can also return a value upon completion of this execution.
FUNCTION
return_type is the type of value returned by the function. (Optional)
Function_name is the identifier by which the function can be called.
Any valid name acceptable to C++ is allowed.
#include <iostream>
using namespace std;
int main () {
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
PASSING ARGUMENTS TO A FUNCTION
Two ways:
int main(){
int x = 20, y = 30;
cout << "In main function before calling change function." << endl << "x: " << x << " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x: " << x << " y: " << y << endl;
}
In main function before calling change function.
x: 20 y: 30
In Change function after changing the values.
x: 150 y: 250
In main function after returning from change function.
x: 20 y: 30
#include <iostream> Passing arguments – Call by value
using namespace std; What is happening?????
int main(){
int x = 20, y = 30;
cout << "In main function before calling change function." << endl << "x: " << x << " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x: " << x << " y: " << y << endl;
}
In main function before calling change function.
x: 20 y: 30
In Change function after changing the values.
x: 150 y: 250
In main function after returning from change function.
x: 150 y: 250
#include <iostream> Passing arguments – Call by reference
using namespace std; What is happening?????
Memory mapping
void change(int &x, int &y){
x = 150;
y = 250; Memory Address
cout << "In Change function after changing the values." << endl << "x: " << x << x = 150
20 0ABC
" y: " << y << endl;
} y = 250
30 0ABD
int main(){
int x = 20, y = 30;
cout << "In main function before calling change function." << endl << "x: " << x
<< " y: " << y << endl;
//calling change function and passing x and y as arguments
change(x,y);
cout << "In main function after returning from change function." << endl << "x:
" << x << " y: " << y << endl;
}
PASSING ARGUMENTS Constant arguments
int main ()
{
string firstName = "Shakib";
string lastName = "Khan";
cout << "After merging the names: " <<concatenate(firstName, lastName) << endl;
return 0;
}
PASSING ARGUMENTS Constant arguments (why to use!)
#include <iostream>
using namespace std;
int main ()
{
string firstName = "Shakib";
string lastName = "Khan";
cout << "After merging the names: " <<concatenate(firstName, lastName) << endl;
return 0;
}
DEFAULT ARGUMENTS
If no values are passed for those parameters the default values are used.
int main ()
{
cout << divide (12) << '\n';
cout << divide (12,4) << '\n';
return 0;
}
FUNCTION OVERLOADING- POLYMORPHISM
More than one Function to have the same name with at least one of the following
distinctions:
#include <iostream>
using namespace std; int main () {
Write a function named swap, that will accept two integers as its argument and swap their values.
Write a main function and call swap function with with integer variables to swap their values and then
print the swapped values in main function.
Sample input output (all print outs must be done in main function):
After Swapping..
x= 40
y=10
Write four overloaded swap function to swap (a) two integers, (b) two floats, (c) two doubles and (d)
two character variables.
Call each of these functions from main function to swap each type of variables.
Submission detail:
- Deadline:17.10.2017
- Email attachment (.txt file)
- Email Subject: assignment3_PL2
- Write your name and id in the email body
For Section A+B1: Send your assignment to the following email address: asiful.islam@aiub.edu
For Section C+B2: Send your assignment to the following email address: sabilah.nishat@gmail.com