In: Computer Science
Q1: What is the difference between function prototype and function definition in C++? Explain.
Q2: What does function prototype consist off and what does function definition consists of? (hint: components)
Q3: Do the names of parameters have to agree in the prototype, definition, and call to the function? Why or why not, explain?
1)
Function Prototype:
A function prototype is a declaration of a function: its name, parameters and return type. Unlike a full definition, the prototype terminates in a semi-colon and it doesn't has body.
int myfunc(double param) ;
The actual names of the parameter values (param in above example) can be left out of the prototype. This gives the flexibility of renaming variables at will.
Function definition:
The function definition tells the compiler what task the function will be performing. A function definition cannot be called unless the function is declared and it has body.
int myfunc(double param){
return param
}
2)
Function Prototype consists of its name, parameters and return type.
Function definition consists of body and can return value to the caller.
3) no need , The actual names of the parameter values (param in above example) can be left out of the prototype. This gives the flexibility of renaming variables at will.
// Screenshot of the code
// Sample output
// Code to copy
#include<iostream>
using namespace std;
// function prototype
int myfunc(double p);
int main(){
// calling function
cout<< myfunc(25);
return 0;
}
// function definition
int myfunc(double param) {
return param*2;
}