







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to function prototypes and definitions in c++ programming. It explains the importance of knowing a function's name, return type, and input parameter types. The document also covers the difference between formal and actual parameters, and discusses pre-defined and user-defined functions. Students will gain a solid understanding of how functions are called and defined, as well as the concept of local and global variables.
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








A function may take input parameters and produce a return value
y = sqrt(9.0);
Function prototype
double sqrt ( double number);
result function parameter formal type name type parameter
Pre-defined or programmer-defined
Terminology
— A function is defined once
Function header
Function body
// Illustrate the function definition and its // use in the main program
#include <iostream.h>
int two_times(int number); // Returns the number multiplied by two
int main() { int x = 10, y = 0;
y = two_times(x); cout << "y is: " << y << endl; y = two_times(y) * 100; cout << "Now y is: " << y << endl; return 0; } int two_times( int number) { return (2 * number); }
A variety of "libraries"
— sqrt(double), pow(double, double), fabs(double)
— abs(int), labs(long)
---------------------------------------
int value; double result; value = 5; result = value/2; // ouch - stores 2.0 - not //what we intended // try result = double (value) / 2; // stores 2.
#include <iostream.h> double total( int number, double price); // Returns the total cost including tax
int main() { double cost, bill; int units = 10;
cost = 50.00; bill = total(units, cost);// the CALL cout << "Total cost is: " << bill << endl; return 0; } double total( int number, double price) { double subtotal; subtotal = number * price; return (subtotal * 1.05); }
Arguments are used for all instances of the formal parameters
that occur in the function body
int prompt_and_read();
// prompt for an integer and return it
int main() { int first_number,second_number ; first_number = prompt_and_read(); // CALL second_number = prompt_and_read(); // CALL
cout << "The numbers read in are:\n" << first_number << "\n" << second_number << "\n"; return 0; }
int prompt_and_read()
{
int value_user_typed; cout << "Input an integer number: " ; cin >> value_user_typed ; return value_user_typed ; }
When writing a function, how do you know you haven’t inadvertantly overwritten a variable, say cost, that was also used by the calling program?
#include <iostream.h> double total( int number, double price); // Returns the total cost including tax
int main() { double cost, bill; int units = 10;
cost = 50.00; bill = total(units, cost); cout << "Unit cost is: " << cost << endl; cout << "Total cost is: " << bill << endl; return 0; } double total( int number, double price) { double cost; cost = number * price return (cost * 1.05); }
The concept of variable scope addresses this issue
#include <iostream.h>
int main () { int x ; char y ; ........................... ............................. return 0 ; }
const double z = 3.14;
int sum () { double x ; double y ; ................. }
int root() { int x; ............................. }