1. C++ Tutorials

What is a function declaration in Cpp

A function features a name and a kind , very similar to a variable. The function’s type is defined by its return value, that is, the worth the function passes back to the program. In addition, the type of arguments required by a function is important.

When a function is declared, the compiler must therefore be given information on.

  • the name and type of the function and
  • the type of each argument

This is also referred to as the function prototype.

int toupper(int);
double pow(double, double);


This informs the compiler that the function toupper() is of type int, i.e. its return
value is of type int, and it expects an argument of type int.

The second function pow() is of type double and two arguments of type double must be passed to the function when it is called.

The types of the arguments could also be followed by names, however, the names are viewed as a comment only.

int toupper(int c);
double pow(double base, double exponent);

From the compiler’s point of view, these prototypes are equivalent to the prototypes
in the previous example. Both junctions are standard junctions.

Standard function prototypes don’t got to be declared, nor should they be, as they have already been declared in standard header files.

If the header file is included in the program’s source code by means of the #include directive, the function can be used immediately.

#include<cmath>

Using Function And Classes Sample program

#include<iostream>
#include<cmath> 

using namespace std;
int main()
{
double x = 2.5, y;
// By means of a prototype, the compiler generates
// the correct call or an error message!
// Computes x raised to the power 3:

y = pow("x", 3.0); // Error! String is not a number
y = pow(x + 3.0); // Error! Just one argument
y = pow(x, 3.0); // ok!
y = pow(x, 3); // ok! The compiler converts the

// int value 3 to double.
cout << "2.5 raised to 3 yields:

<< y << endl;
// Calculating with pow() is possible:
cout << "2 + (5 raised to the power 2.5) yields: "
<< 2.0 + pow(5.0, x) << endl;
return 0;
}

Screen output
2.5 raised to the power 3 yields: 15.625
2 + (5 raised to the power 2.5) yields: 57.9017

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Do you like Zubeen's articles?  Follow on social!
Comments to: What is a function declaration in Cpp

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

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password