KEMBAR78
Functions in C++ programming language.pptx
{
}
The Presentation Owners :
Alan Mahmood
Sumaya Mohamed
Rebin Faisal
Rozh Ebrahim
Zina Maaruf
...
Functions In C++
Supervised By:
Dr. Chiman Haidar
Contents :
• Introduction About Function In C++
• Function Declaration And Definition
• Parameters In C++ Function
• Return Task In Function
• Default Argument
• Function Overloading
• Some Libraries In C++ Function
• Program Example 1
• Program Example 2
Introduction About {Functions}
A function is a set of statements
that takes input, does some
specific computation, and
produces output. The idea is to put
some commonly or
repeatedly done tasks together to
make a {function} so that instead
of writing the same code again
and again for different inputs, we
can call this function.
In simple terms, a function is a
block of code that runs only when
it is called.
Function
Declaration And
Definition
// Function declaration
int add(int a, int
b);
// Function definition
int add(int a, int b)
{
return a + b;
}
}
{
Parameters
In Function A parameter is a special kind of
variable used in a function to refer
to one of the pieces of data
provided as input to the function.
These pieces of data are the values
of the arguments with which the
function is going to be
called/invoked.
Parameters In {Functions}
A parameter is a special
kind of variable used in
a function to refer to
one of the pieces of data
provided as input to the
function. These pieces
of data are the values of
the arguments with
which the function is
going to be
#include <iostream>
// Function returning an integer
int square(int num) {
return num * num;
}
// Function returning a double
double calculateAverage(double a, double b) {
return (a + b) / 2.0;
}
Return Task {Functions}
Default Argument in {Functions}
#include <iostream>
// Function with default argument
void printNumber(int num = 10) {
std::cout << "Number: " << num << std::endl;
}
int main() {
// Function call without providing an argument
printNumber(); // Uses the default value (10)
return 0;
}
{Function} Overloading
Function overloading is a feature of object-oriented
programming where two or more functions can have the same
name but different parameters. When a function name is
overloaded with different jobs it is called Function Overloading.
In Function Overloading “Function” name should be the same
and the arguments should be different. Function overloading
can be considered as an example of a polymorphism feature in
C++.
{Function} Overloading
#include <iostream>
// Function overloading
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
Call The Functions
Into Main Function
{Function} Overloading
int main() {
int result_int = add(3, 4);
double result_double = add(3.5, 4.2);
std::cout << "Sum (int): " << result_int << std::endl;
std::cout << "Sum (double): " << result_double <<
std::endl;
return 0;
}
Some Libraries Of {Functions} In C++
Stdio.h Conio.h String.h
Math.h Ctype.h Time.h
Program Example 1
#include <iostream>
#include <cmath>
Using namespace std;
// Function to calculate the area of a rectangle
double calculateRectangleArea(double length, double width) {
return length * width;
}
// Function to calculate the area of a circle
double calculateCircleArea(double radius) {
return Pi * pow(radius, 2);
}
}
Out Of the Main
Function
A program That measures The area of
rectangle and a circuit By user :
int main() {
// Input for rectangle
double rectLength, rectWidth;
cout << "Enter length and width of the rectangle: ";
cin >> rectLength >> rectWidth;
// Calculate and display rectangle area
cout << "Area of the rectangle: " << calculateRectangleArea(rectLength, rectWidth) << endl;
// Input for circle
double circleRadius;
cout << "Enter the radius of the circle: ";
cin >> circleRadius;
// Calculate and display circle area
cout << "Area of the circle: " << calculateCircleArea(circleRadius) << endl;
return 0;
}
The Output By user
Enter length and width of the rectangle: 53
Area of the rectangle: 15
Enter the radius of the circle: 2
Area of the circle: 12.5664
Program Example 2
#include <iostream>
#include <string>
Using namespace std;
int main() {
// Declare and initialize a string
string greeting = "Hello ";
// Concatenate another string
greeting += "World!";
// Display the concatenated string
cout << greeting << endl;
// Find the length of the string
cout << "Length of the string: " << greeting.length() << endl;
12
compiler
Hello world!
Compiler
A program to determine
string length and first
character and last character
and string attend
condition(if)
// Access individual characters in the string
cout << "First character: " << greeting[0] << endl;
cout << "Last character: " << greeting.back() << endl;
// Check if the string is empty
if (greeting.empty()) {
cout << "The string is empty." << endl;
} else {
cout << "The string is not empty." << endl;
}
return 0;
}
The Output
• Hello, World!
• Length of the string: 12
• First character: H
• Last character: !
• The string is not empty


Functions in C++ programming language.pptx

  • 1.
    { } The Presentation Owners: Alan Mahmood Sumaya Mohamed Rebin Faisal Rozh Ebrahim Zina Maaruf ... Functions In C++ Supervised By: Dr. Chiman Haidar
  • 2.
    Contents : • IntroductionAbout Function In C++ • Function Declaration And Definition • Parameters In C++ Function • Return Task In Function • Default Argument • Function Overloading • Some Libraries In C++ Function • Program Example 1 • Program Example 2
  • 3.
    Introduction About {Functions} Afunction is a set of statements that takes input, does some specific computation, and produces output. The idea is to put some commonly or repeatedly done tasks together to make a {function} so that instead of writing the same code again and again for different inputs, we can call this function. In simple terms, a function is a block of code that runs only when it is called.
  • 4.
    Function Declaration And Definition // Functiondeclaration int add(int a, int b); // Function definition int add(int a, int b) { return a + b; }
  • 5.
    } { Parameters In Function Aparameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be called/invoked.
  • 6.
    Parameters In {Functions} Aparameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be
  • 7.
    #include <iostream> // Functionreturning an integer int square(int num) { return num * num; } // Function returning a double double calculateAverage(double a, double b) { return (a + b) / 2.0; } Return Task {Functions}
  • 8.
    Default Argument in{Functions} #include <iostream> // Function with default argument void printNumber(int num = 10) { std::cout << "Number: " << num << std::endl; } int main() { // Function call without providing an argument printNumber(); // Uses the default value (10) return 0; }
  • 9.
    {Function} Overloading Function overloadingis a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading can be considered as an example of a polymorphism feature in C++.
  • 10.
    {Function} Overloading #include <iostream> //Function overloading int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } Call The Functions Into Main Function
  • 11.
    {Function} Overloading int main(){ int result_int = add(3, 4); double result_double = add(3.5, 4.2); std::cout << "Sum (int): " << result_int << std::endl; std::cout << "Sum (double): " << result_double << std::endl; return 0; }
  • 12.
    Some Libraries Of{Functions} In C++ Stdio.h Conio.h String.h Math.h Ctype.h Time.h
  • 13.
    Program Example 1 #include<iostream> #include <cmath> Using namespace std; // Function to calculate the area of a rectangle double calculateRectangleArea(double length, double width) { return length * width; } // Function to calculate the area of a circle double calculateCircleArea(double radius) { return Pi * pow(radius, 2); } } Out Of the Main Function A program That measures The area of rectangle and a circuit By user :
  • 14.
    int main() { //Input for rectangle double rectLength, rectWidth; cout << "Enter length and width of the rectangle: "; cin >> rectLength >> rectWidth; // Calculate and display rectangle area cout << "Area of the rectangle: " << calculateRectangleArea(rectLength, rectWidth) << endl; // Input for circle double circleRadius; cout << "Enter the radius of the circle: "; cin >> circleRadius; // Calculate and display circle area cout << "Area of the circle: " << calculateCircleArea(circleRadius) << endl; return 0; }
  • 15.
    The Output Byuser Enter length and width of the rectangle: 53 Area of the rectangle: 15 Enter the radius of the circle: 2 Area of the circle: 12.5664
  • 16.
    Program Example 2 #include<iostream> #include <string> Using namespace std; int main() { // Declare and initialize a string string greeting = "Hello "; // Concatenate another string greeting += "World!"; // Display the concatenated string cout << greeting << endl; // Find the length of the string cout << "Length of the string: " << greeting.length() << endl; 12 compiler Hello world! Compiler A program to determine string length and first character and last character and string attend condition(if)
  • 17.
    // Access individualcharacters in the string cout << "First character: " << greeting[0] << endl; cout << "Last character: " << greeting.back() << endl; // Check if the string is empty if (greeting.empty()) { cout << "The string is empty." << endl; } else { cout << "The string is not empty." << endl; } return 0; }
  • 18.
    The Output • Hello,World! • Length of the string: 12 • First character: H • Last character: ! • The string is not empty
  • 19.