More ‘concepts'on Function
More ‘concepts’ on Function
Declaration vs. definitionBefore a name (identifier) can be used, it has to be declared:telthecompilerthetype:int a; double b;Mostdeclarationsarealsodefinitions:allocationof memoryAfunctionis declared by writing down its interface (prototype)Adeclarationcanappearmanytimes (evenlocally)Compilerdoes notgeneratethe codesltonly checks typesandnumber ofargumentsAfunctionis defined bywriting down its header and bodyAdefinition isdoneonlyonceCompilergeneratesthecodes
Declaration vs. definition A function is declared by writing down its interface (prototype) A declaration can appear many times (even locally) Compiler does not generate the codes It only checks types and number of arguments A function is defined by writing down its header and body A definition is done only once Compiler generates the codes Before a name (identifier) can be used, it has to be declared: tell the compiler the type: int a; double b; Most declarations are also definitions: allocation of memory
Afunction must be declared before it can beusedAll functions(likemainO)‘see'eachotherDon't“nest'functions!(you cannot unlike Pascal)All functionsare atthe samelevel,global!
A function must be declared before it can be used All functions (like main()) ‘see’ each other Don’t ‘nest’ functions! (you cannot unlike Pascal) All functions are at the same level, global!
Function PrototypeThefunction prototype declares the interface,or inputand output parameters of the function, leaving theimplementation forthefunctiondefinitionThis separates the concept (declaration')from theimplementation(definition)!This leads to'separate compilation'as well!
Function Prototype The function prototype declares the interface, or input and output parameters of the function, leaving the implementation for the function definition. This separates the concept (‘declaration’) from the implementation (‘definition’)! This leads to ‘separate compilation’ as well!
#include <iostream>using namespace std;// Define a function to take absolute value of an integerint absolute(int fx)1int abs;if (fx>0)abs=fxiabs=-fxielsereturn(abs);int main()(int x,y,diff;cout << "Enter two integers (separated by a blank): ";cin >> x >>y;diff x-yidiff =absolute(diff);cout <<"rhe absolute difference between"<x<"and"<y<is:"diffendl,return O;#include<iostream>using namespace std;intabsolute(int): //function prototype for absolute()int main(/int x, y, diff;cout << "Enter two integers (separated by a blank): ";cin >> x >>yidiff=x-yiEquivalent!diff -absolute(diff):cout << "The absolute difference between "<<x<and<<y<<is:<diff<endl,return;// Define a function to take absolute value of an integerint absolute(int fx)3int abs;if (fx >= 0) abs=fxiabs=-fx;elsereturm(abs);
#include <iostream> using namespace std; // Define a function to take absolute value of an integer int absolute(int fx) { int abs; if (fx >= 0) abs=fx; else abs= -fx; return (abs); } int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x-y; diff = absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } #include <iostream> using namespace std; int absolute (int); // function prototype for absolute() int main(){ int x, y, diff; cout << "Enter two integers (separated by a blank): "; cin >> x >> y; diff = x-y; diff = absolute(diff); cout << "The absolute difference between " << x << " and " << y << " is: " << diff << endl; return 0; } // Define a function to take absolute value of an integer int absolute(int fx) { int abs; if (fx >= 0) abs=fx; else abs= -fx; return (abs); } Equivalent!