Are C++ function declarations optional? - c++

I just noticed something when creating functions. In the code:
#include <iostream>
using namespace std;
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
it will work because the function being called is on top of the caller, but if I put the function add() below the calling function in main() it won't work.
#include <iostream>
using namespace std;
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
and the compiler will tell me that the identifier add() cannot be found.
so why do we declare functions anyway? like this:
#include <iostream>
using namespace std;
int add(int a, int b = 20);
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b)
{
int r;
r = a + b;
return (r);
}

A definition is implicitly a declaration. And a declaration must come ahead of the use.

All functions need to be declared before they are used.
You can do that by either (1) writing a declaration, or (2) writing a definition.
Relying solely on (2) can be tempting but then you are bound to order your program in a particular way, and is occasionally impossible. For example, the following will not compile unless the comment is removed.
//void bar(int);
void foo(int n)
{
if (!n){
bar(n);
}
}
void bar(int n)
if (n){
foo(n);
}
}
int main()
{
foo(1);
}

No.
If the function definition appears before the function call, then prototype is not mandatory. Otherwise function prototype is necessary to let compiler know how to respond to a function when it is called.

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
if the function definition appears after the function call then prototype is mandatory. because it tells the compiler to how to respond the function when it is called.
check the following example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int); // function prototype
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
and if the function definition is made before the function call then it is not mandatory to declare function prototype.
consider example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}

Related

make prototype of overloading function c++

I want to make a overloading function with a prototype in C++.
#include <iostream>
using namespace std;
int rectangle(int p, int l);
int main() {
cout << rectangle(3);
return 0;
}
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
I got error at
int rectangle(int p, int l);
is that possible make prototype with a overloading function? if possible how to do it
You've to declare the function before you use/call it. You did declare the 2 argument version of rectangle function but you seem to forget to declare the 1 argument taking version.
As shown below if you add the declaration for the 1 argument version then your program works(compiles).
#include <iostream>
using namespace std;
//declare the function before main
int rectangle(int p, int l);
int rectangle(int p);//ADDED THIS DECLARATION
int main() {
cout << rectangle(3);
return 0;
}
//define the functions after main
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
The output of the program can be seen here.
Alternative solution:
If you don't want to declare each function separately then you should just define them before main instead of declaring them as shown below.
#include <iostream>
using namespace std;
//define the functions before main. This way there is no need to write a separate function declaration because all definition are declarations
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
int main() {
cout << rectangle(3);
return 0;
}

do we have access to variables inside an INLINE function after its execution has ended,like what we don't have in normal user defined function?

calculation() function is not working when m making input() function outside the class...has it got something to do with inline function??
#include <iostream>
using namespace std;
class price
{
public:
int pen;
int rubber;
int scale;
void input()
{
cout<<"enter the variables\n";
cin>>pen>>rubber>>scale;
cout<<"\n"<<pen<<" "<<rubber<<" "<<scale;
}
};
void calculate(price p)
{
int rate[2],total;
rate[0]=p.pen*5;
rate[1]=p.rubber*3;
rate[2]=p.scale*4;
total=rate[0]+rate[1]+rate[2];
cout<<"\n"<<total;
}
int main()
{
price a,b,c;
a.input();
calculate(a);
return 0;
}
No we don't. inline has no effect at all on the semantics of a C++ function. It's only effect is on how that function is treated by the linker.

Obtain variables in function from other function C++

I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}

Passing member function as parameter to another function in c++

I have the following problem.
I have a base abstract class with a pure virtual method, and I want to pass it as an argument to another member function(so not a normal function). Yet I have an error when trying to call the method with specified function. Code speaks better than words so bellow I have posted the code that generates the problem
class BaseClass
{
public:
BaseClass();
int add(int, int);
virtual void op(void (*f)(int, int), string s, int a, int b) = 0;
~BaseClass();
};
#include "BaseClass.h"
class ClasaDerviata:public BaseClass
{
public:
ClasaDerviata();
void testNumere(int a, int b);
void op(void(*f)(int, int), string s, int a, int b);
~ClasaDerviata();
};
#include "BaseClass.h"
BaseClass::BaseClass()
{
}
int BaseClass::add(int a, int b)
{
return a + b;
}
BaseClass::~BaseClass()
{
}
#include "ClasaDerviata.h"
#include <iostream>
using namespace std;
ClasaDerviata::ClasaDerviata()
{
}
void ClasaDerviata::testNumere(int a, int b)
{
cout << a + b << "\n";
cout << " suma " << add(a,b) << "\n";
}
void ClasaDerviata :: op (void (*f)(int, int), string s, int a, int b)
{
f(a, b);
cout << s << "\n";
}
ClasaDerviata::~ClasaDerviata()
{
}
#include "ClasaDerviata.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ClasaDerviata *a;
a = new ClasaDerviata();
int x, y;
cin >> x >> y;
a->op(&ClasaDerviata::testNumere, "test metoda", x, y);
system("pause");
return 0;
}
Thank you for your time!
void ClasaDerviata::testNumere(int a, int b); is not of type void (*)(int, int) but void (ClasaDerviata::*)(int, int)
You may add static to testNumere and add to fix your problem or change signature of your function (and change internal code too).
Remember when you make a call to a member function a hidden parameter 'this' is passed. So your ClasaDerviata::testNumere(int a, int b); function actually takes three parameters.
I would suggest to read Joseph Garvin comment in
How can I pass a class member function as a callback?
he has explained it very well.

Error: "undefined reference to 'function'" in C++

I got an error while compiling C++:
/tmp/ccqs6UN2.o: In function main': PowerModulus.cpp:(.text+0x194): undefined reference to takeModulusLOOP(int, int, int)' collect2: ld returned 1 exit status
The source code:
#include "PowerModulus.h"
#include <iostream>
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
int main() {
std::cout << takeModulusLOOP(5348, 700, 335);
}
int PowerModulus::takeModulusLOOP(int x, int n, int moduint) {
int total = modint(x, moduint);
n--;
while (--n) {
total = modint(total * x, moduint);
}
return total;
}
int PowerModulus::modint(int x, int moduint) {
while (x < 0) // Deal with negative
x += moduint;
return x % moduint; // Comes out positive now -> %
}
PowerModulus::PowerModulus() {
// TODO Auto-generated constructor stub
}
PowerModulus::~PowerModulus() {
// TODO Auto-generated destructor stub
}
Header file:
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
class PowerModulus {
public:
int takeModulusLOOP(int x, int n, int moduint);
int modint(int x, int moduint);
PowerModulus();
virtual ~PowerModulus();
};
#endif /* POWERMODULUS_H_ */
Where is the error?
You have declared a global takeModulusLOOP function, then call it in main, without ever defining it. This is a different function than PowerModulus::takeModulusLOOP.
// main.cpp
#include "PowerModulus.h"
#include <iostream>
int main(){
std::cout << PowerModulus::takeModulusLOOP(5348,700,335) << '\n';
return 0;
}
Changed to a namespace instead of a class, and separated into header and implementation (instead of grouping in main.cpp):
// PowerModulus.cpp
#include "PowerModulus.h"
namespace PowerModulus {
int takeModulusLOOP(int x, int n, int moduint){
int total = modint(x, moduint) ;
n--;
while (--n){
total = modint( total * x, moduint );
}
return total;
}
int modint(int x, int moduint){
while ( x < 0) // deal with negative
x += moduint;
return x % moduint;//comes out positive now -> %
}
}
Header:
// PowerModulus.h
#ifndef POWERMODULUS_H_
#define POWERMODULUS_H_
namespace PowerModulus {
int modint(int x, int moduint);
int takeModulusLOOP(int x, int n, int moduint);
}
#endif
This line:
std::cout << takeModulusLOOP(5348,700,335);
is calling the non-class takeModulusLOOP, which you haven't defined anywhere.
You should either call the class version, by providing an object of the class type and using something like:
PowerModulus p;
std::cout << p.takeModulusLOOP(5348,700,335);
(most likely) or providing a non-class version (least likely).
You could also consider making the function static since it doesn't seem to require an object at all. Then you don't need to instantiate one.
You receive the error, because you do not have such a function.
Actually, you have it in PowerModulus class, so you should call the function from PowerModulus instance.
PowerModulus pM;
pM.takeModulusLoop(5348,700,335);
You do not need to claim the function in the beginning of your .h file or in the beginning of your .cpp file.
If you intended to use the takeModulusLoop function of the PowerModulus class then you need not declare a global function again...
But, if you intended to use a different global function, then you need to define it in its context...