Lecture 7

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Introduction To

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;

void student(string name, int mark) {


cout<<"Student Name:"<< name <<" , "<<"Marks:"<<mark<<endl;
}

int main() {
student("Sopno", 18);
student("Padma", 19);
student("Shetu", 20);
return 0;
}
Return Values

#include <iostream> #include <iostream> #include <iostream> #include <iostream>


using namespace std; using namespace std; using namespace std; using namespace std;

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;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy