Lecture 7
Lecture 7
Lecture 7
Programming
Contents
• Function
• Function Parameters
• Multiple Parameters
• Return Values
• Function Overloading
• Pass by Value & Pass by Reference
Function
• C++ provides some pre-defined functions, such as main(), which is
used to execute code. But you can also create your own functions to
perform certain actions.
• To create a function, specify the name of the function, followed by
parentheses ():
• Syntax-
void functionName()
{
// code to be executed
}
Function...
#include <iostream>
#include <iostream>
#include <iostream> using namespace std;
using namespace std;
using namespace std;
void newFunction()
void newFunction();
void newFunction() {
{ cout << "I am learning function";
int main()
cout << "This is my function"; }
{
}
newFunction();
int main()
return 0;
int main() {
}
{ newFunction();
void newFunction()
newFunction(); cout << endl;
{
return 0; newFunction();
cout << "I am learning function";
} return 0;
}
}
Function Parameters
• Information can be passed to functions as a parameter. Parameters act
as variables inside the function.
• Parameters are specified after the function name, inside the
parentheses. You can add as many parameters as you want, just
separate them with a comma.
• Syntax-
void functionName(parameter1, parameter2, parameter3,…..)
{
// code to be executed
}
Function Parameters...
• When a parameter is passed to the function, it is called an argument.
• A parameter with a default value, is often known as an optional
parameter.
• When working with multiple parameters, the function call must have
the same number of arguments as there are parameters, and the
arguments must be passed in the same order.
• If you want the function to return a value, you can use a data type
instead of void, and use the return keyword inside the function.
• With function overloading, multiple functions can have the same
name with different parameters.
Function Parameters...
#include <iostream>
#include <iostream> using namespace std;
using namespace std;
void myCourse(string course = "C++")
void studentId(int id) {
{ cout << course << endl;
cout <<"22-"<< id <<"-2"<<endl; }
} int main()
{
int main() { myCourse("C");
studentId(47892); myCourse("Java");
studentId(47893); myCourse();
studentId(47894); myCourse("C#");
return 0; return 0;
} }
Multiple Parameters
#include <iostream>
using namespace std;
int main() {
student("Sopno", 18);
student("Padma", 19);
student("Shetu", 20);
return 0;
}
Return Values
int number(int x) int number(int x) int numbers(int a, int b) int numbers(int a, int b)
{ { { {
return x; return x + 2; return a * b; return a + b;
} } } }
int main()
int main() int main() int main() {
{ { { int add = numbers(10, 20);
cout << number(5); cout << number(5); cout << numbers(10, 20); cout <<"Addition is:"<<add;
return 0; return 0; return 0; return 0;
} } } }
Function Overloading
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
int num(int a, int b) {
int num1(int a, int b) {
return a + b;
return a + b;
}
}
double num(double a, double b) {
float num2(float a, float b) {
return a + b;
return a + b;
}
}
int main()
int main() {
{
int marks1 = num1(10, 20);
int num1 = num(10, 5);
float marks2 = num2(5.5, 4.7);
double num2 = num(5.5, 5.4);
cout <<"Integer Value:"<< marks1 <<endl;
cout << "Integer Number:" << num1 <<endl;
cout <<"Float Value:"<< marks2;
cout << "Double Number:" << num2;
return 0;
return 0;
}
}
Pass by Value & Pass by Reference
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
void marks(int *num)
void marks(int num)
{
{
*num= 50;
num= 50;
}
}
int main()
int main()
{
{
int a = 30;
int a = 30;
cout<<"Before Calling:"<<a << endl;
cout<<"Before Calling:"<<a << endl;
marks(&a);
marks(a);
cout<<"After Calling:"<<a;
cout<<"After Calling:"<<a;
return 0;
return 0;
}
}