C++ Functions

functions

With the aid of examples, we will learn about C++ functions and function expressions in this tutorial.

The only time a function runs is when it is called. A function can accept data that you pass in as parameters.

Functions are used to carry out specific actions and are crucial for code reuse: One code definition can be used multiple times. Our program is simple to understand and reusable because it breaks a complex problem down into smaller components.

There are two types of functions:

  • Standard Library Functions: C++ predefined
  • User-defined Function: User-generated

We’ll concentrate primarily on user-defined functions in this tutorial.

User-defined Function in C++

In C++, programmers are able to create their own functions.

Code for carrying out a particular task is gathered into a group by a user-defined function, which is given a name (identifier).

The codes defined in the function’s body are all executed when the function is called from any other part of the program.

Create a function

Some pre-defined functions are available in C++, such as main(), which is used to run programs. However, you can also design your own custom functions to carry out specific tasks.

Put parentheses () after the function name to create (often referred to as declare) a function:

Syntax:

void GeektoCodefunction() {
  // code to be executed
}

Explained Example

  • The function’s name is GeektoCodefunction().
  • Void signifies that the function has no return value. Return values will be covered in more detail in the following chapter.
  • Add the code that specifies what the function should do inside the function (the body).

Invoke a function

Declared functions are not immediately carried out. They are “kept for later use” and will run when summoned later.

Write the function’s name, two parentheses (), and a semicolon (;) to call the function.

geektocodefunction() is used in the example below to print a text (the action) when it is called:

Example:

Inside main(), call geektocodefunction():

// Create a function
void geektocodefunction() {
  cout << "I just got executed!";
}

int main() {
  geektocodefunction(); // call the function
  return 0;
}
OUTPUT:
// Outputs "I just got executed!"

A function can be called multiple times:

void myFunction() {
  cout << "just executed!\n";
}

int main() {
  myFunction1();
  myFunction2();
  myFunction3();
  return 0;
}
OUTPUT: 
just executed!
just executed!
 just executed!

Note: The function’s call-back arguments must be of the same type as the corresponding parameters specified in the function declaration.

Advantages of User-Defined Functions

  • Code is made reusable by functions. They can be declared once and used repeatedly.
  • The program is made simpler by functions because each small task is separated into one.
  • Functions make text easier to read.

Library Functions in C++

  • In C++ programming, library functions are equivalent to built-in functions.
  • Instead of writing their own functions, programmers can use library functions by calling them directly.
  • The C++ library functions sqrt(), abs(), isdigit(), etc. are some examples.
  • The header file in which these library functions are defined must typically be included in order to use them.
  • For instance, we need to include the header file cmath in order to use mathematical functions like sqrt() and abs().

Example:  C++ Program to Find the Square Root of a Number

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double number, sqreRoot;
    
    number = 36.0;

    // sqrt() is a library function to calculate the square root
    sqreRoot = sqrt(number);

    cout << "Square root of " << number << " = " << sqreRoot;

    return 0;
}
OUTPUT:
Square root of 36 = 6 
  • The sqrt() library function is employed in this program to determine a number’s square root.
  • The cmath header file contains a definition of the sqrt() function declaration. Because of this, in order to use the sqrt() method, we must use the code #include <cmath>.

Leave a Reply

Your email address will not be published. Required fields are marked *