Software Development Exam  >  Software Development Notes  >  Basics of C++  >  Functors in C++

Functors in C++ | Basics of C++ - Software Development PDF Download

Introduction

C++ is a popular programming language that supports the use of functions to perform specific tasks. A function is a block of code that can be reused multiple times in a program, making code modular and easier to manage. Functions in C++ make programming easier and more efficient.

Defining a Function

In C++, a function is defined by specifying its return type, name, and parameters. The general syntax for defining a function is as follows:

return_type function_name(parameters) {

   // function body

}

  • The return_type specifies the data type that the function returns. If the function does not return anything, use the 'void' keyword.
  • The function_name is the name of the function, and it should be meaningful to the task the function performs.
  • The parameters are optional and can be used to pass values to the function.

Example: 

#include <iostream>

using namespace std;


// Function declaration

void greet();


// Main function

int main() {

   // Function call

   greet();

   return 0;

}


// Function definition

void greet() {

   cout << "Hello, World!" << endl;

}

Output

Hello, World!

In the example above, we define a function named 'greet' that takes no parameters and returns nothing. We then call the function inside the 'main' function using 'greet();'. This function prints "Hello, World!" to the console when called.

Passing Parameters

Functions in C++ can accept parameters to perform specific tasks. Parameters can be of any data type, including integers, floats, doubles, characters, and arrays. When a function is called, values can be passed as arguments to the function's parameters.

Example: 

#include <iostream>

using namespace std;


// Function declaration

void greet(string name);


// Main function

int main() {

   // Function call

   greet("John");

   greet("Mary");

   return 0;

}


// Function definition

void greet(string name) {

   cout << "Hello, " << name << "!" << endl;

}

Output

Hello, John!

Hello, Mary!

In the example above, we define a function named 'greet' that takes a string parameter named 'name'. We then call the function inside the 'main' function using 'greet("John")'; and 'greet("Mary");'. This function prints "Hello, John!" and "Hello, Mary!" to the console when called.

Returning Values

Functions in C++ can also return values to the calling function. The return type of the function should be specified in the function declaration and definition. To return a value, use the 'return' keyword followed by the value to be returned.

Example:

#include <iostream>

using namespace std;


// Function declaration

int square(int num);


// Main function

int main() {

   int result = square(5);

   cout << "The square of 5 is " << result << endl;

   return 0;

}


// Function definition

int square(int num) {

   return num * num;

}

Output

The square of 5 is 25

In the example above, we define a function named 'square' that takes an integer parameter named 'num' and returns an integer value that is the square of the input. We then call the function inside the 'main' function using 'int result = square(5)'; and output the result using 'cout << "The square of 5 is " << result << endl;'. This function returns the square of the input number.

Sample Problems

  • Write a function in C++ to calculate the areaSample Problems:
  • Write a function in C++ to calculate the area of a circle.

#include <iostream>

using namespace std;


// Function declaration

double area(double radius);


// Main function

int main() {

   double r = 5.0;

   double result = area(r);

   cout << "The area of the circle with radius " << r << " is " << result << endl;

   return 0;

}


// Function definition

double area(double radius) {

   return 3.14159 * radius * radius;

}

Output

The area of the circle with radius 5 is 78.5398

2. Write a function in C++ to check if a number is even or odd.

#include <iostream>

using namespace std;


// Function declaration

bool isEven(int num);


// Main function

int main() {

   int n = 5;

   if (isEven(n)) {

      cout << n << " is even." << endl;

   } else {

      cout << n << " is odd." << endl;

   }

   return 0;

}


// Function definition

bool isEven(int num) {

   if (num % 2 == 0) {

      return true;

   } else {

      return false;

   }

}

Output

5 is odd.

Conclusion

Functions in C++ are an essential tool for writing efficient, modular, and readable code. They allow developers to write code that is reusable and easier to manage. C++ supports various types of functions, including those that return values, take parameters, or both. By understanding the syntax and usage of functions in C++, beginners can write more complex and useful programs.

The document Functors in C++ | Basics of C++ - Software Development is a part of the Software Development Course Basics of C++.
All you need of Software Development at this link: Software Development
70 videos|45 docs|15 tests

Top Courses for Software Development

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

study material

,

shortcuts and tricks

,

ppt

,

Sample Paper

,

MCQs

,

Extra Questions

,

Important questions

,

Functors in C++ | Basics of C++ - Software Development

,

mock tests for examination

,

video lectures

,

pdf

,

Summary

,

practice quizzes

,

Functors in C++ | Basics of C++ - Software Development

,

Viva Questions

,

Objective type Questions

,

Previous Year Questions with Solutions

,

Exam

,

Free

,

Semester Notes

,

Functors in C++ | Basics of C++ - Software Development

;