EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  C++ for EmSAT Achieve  >  Functions in C++

Functions in C++ | C++ for EmSAT Achieve PDF Download

Introduction

C++ is a popular programming language used for developing a wide range of applications. It is an object-oriented programming language that allows you to create classes, objects, and functions. Functions are an essential part of any programming language, including C++. A function is a group of statements that perform a specific task. Functions help to break down the code into smaller, more manageable parts, making the code more readable and reusable. In this article, we will discuss functions in C++ and how to use them.

Creating a Function

To create a function in C++, you need to follow these steps:

  • Declare the function: The declaration of the function tells the compiler about the name of the function, its return type, and the types of parameters it expects.
  • Define the function: The definition of the function contains the actual code that is executed when the function is called.

Here is an example of a simple function that adds two numbers:

int addNumbers(int a, int b) {

   int sum = a + b;

   return sum;

}

In this example, we have declared a function called 'addNumbers' that takes two integer parameters ('a' and 'b'). The function then calculates the sum of these two numbers and returns the result.

Calling a Function

Once you have created a function, you can call it from another part of your code. To call a function, you need to use its name and provide any required parameters.
Here is an example of how to call the 'addNumbers' function:

int result = addNumbers(5, 7);

In this example, we are calling the 'addNumbers' function with two integer values (5 and 7). The result of this function call is then assigned to the variable 'result'.

Function Parameters

Functions can take one or more parameters. Parameters are used to pass values to the function. You can declare the type and name of each parameter in the function declaration.
Here is an example of a function that takes two parameters:

void printNumbers(int a, int b) {

   cout << "a = " << a << endl;

   cout << "b = " << b << endl;

}

In this example, we have declared a function called 'printNumbers' that takes two integer parameters ('a' and 'b'). The function then prints the value of each parameter to the console.

Function Return Type

Functions can return a value or not. If a function does not return a value, then its return type is 'void'. If a function returns a value, then you must declare the type of the value that the function will return.
Here is an example of a function that returns a value:

int square(int x) {

   return x * x;

}

In this example, we have declared a function called 'square' that takes an integer parameter ('x'). The function then calculates the square of the parameter and returns the result as an integer.

Function Overloading

Function overloading is a feature in C++ that allows you to create multiple functions with the same name but with different parameters. The compiler will choose the appropriate function to call based on the parameters that are passed to the function.
Here is an example of function overloading:

int addNumbers(int a, int b) {

   return a + b;

}


double addNumbers(double a, double b) {

   return a + b;

}

In this example, we have declared two functions called 'addNumbers'. One function takes two integer parameters ('a' and 'b'), while the other function takes two double parameters ('a' and 'b'). The compiler will choose the appropriate function to call based on the types of the parameters that are passedSample Problems:

1. Write a C++ function to calculate the factorial of a given number.

int factorial(int n) {

   if (n == 0) {

      return 1;

   } else {

      return n * factorial(n - 1);

   }

}

In this example, we have declared a function called 'factorial' that takes an integer parameter ('n'). The function then calculates the factorial of the parameter recursively using the formula 'n! = n * (n-1)!'. The function returns the result as an integer.

2. Write a C++ function to find the maximum of three numbers.

int max(int a, int b, int c) {

   int max = a;

   if (b > max) {

      max = b;

   }

   if (c > max) {

      max = c;

   }

   return max;

}

In this example, we have declared a function called 'max' that takes three integer parameters ('a', 'b', and 'c'). The function then compares each parameter to find the maximum value and returns it as an integer.

3. Write a C++ function to check if a given string is a palindrome.

bool isPalindrome(string str) {

   int length = str.length();

   for (int i = 0; i < length/2; i++) {

      if (str[i] != str[length - i - 1]) {

         return false;

      }

   }

   return true;

}

In this example, we have declared a function called 'isPalindrome' that takes a string parameter ('str'). The function then compares the characters in the string to check if it is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward. The function returns 'true' if the string is a palindrome, and 'false' otherwise.

Conclusion


Functions are an essential part of any programming language, including C++. They help to break down the code into smaller, more manageable parts, making the code more readable and reusable. In this article, we discussed how to create, call, and use functions in C++. We also covered function parameters, return types, and function overloading. Finally, we provided some sample problems to demonstrate how functions can be used to solve real-world problems.
The document Functions in C++ | C++ for EmSAT Achieve is a part of the EmSAT Achieve Course C++ for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
70 videos|45 docs|15 tests

Top Courses for EmSAT Achieve

70 videos|45 docs|15 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

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

Functions in C++ | C++ for EmSAT Achieve

,

study material

,

video lectures

,

Functions in C++ | C++ for EmSAT Achieve

,

Free

,

Exam

,

Objective type Questions

,

Extra Questions

,

shortcuts and tricks

,

practice quizzes

,

pdf

,

Sample Paper

,

Semester Notes

,

Functions in C++ | C++ for EmSAT Achieve

,

Summary

,

Previous Year Questions with Solutions

,

ppt

,

Important questions

,

Viva Questions

,

MCQs

,

past year papers

,

mock tests for examination

;