Understanding Input Parameters and Return Values in C++, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/31/2009

koofers-user-agl
koofers-user-agl 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS120: week 5 slide(1)
Function Prototype
A function may take input parameters and produce a return value
need to know its name, type of result, type of each parameter
y = sqrt(9.0);
Function prototype
describes how the function is called
tells everything you need to know to make a function call
double sqrt (double number);
result function parameter formal
type name type parameter
formal parameter is a place holder to stand for the
actual parameter
function prototype terminates with semi-colon
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Understanding Input Parameters and Return Values in C++ and more Study notes Computer Science in PDF only on Docsity!

Function Prototype

A function may take input parameters and produce a return value

  • need to know its name, type of result , type of each parameter

y = sqrt(9.0);

Function prototype

  • describes how the function is called
  • tells everything you need to know to make a function call

double sqrt ( double number);

result function parameter formal type name type parameter

  • formal parameter is a place holder to stand for the actual parameter
  • function prototype terminates with semi-colon

Function Definition

Pre-defined or programmer-defined

  • Predefined functions in libraries: a partial list on p. 105
  • Programmer-defined functions: need to specify the function prototype and function definition

Terminology

  • A function is defined using a function header and function body

— A function is defined once

Function header

  • written exactly the same as the function prototype, except ";"
  • declares the type of return value, name of the function, type of each parameter, and name of each formal parameter

Function body

  • follows the function header and completes the function definition
  • consists of declarations and executable statements -- similar to the body of the main part of the program

Simple Value-Returning Function

// 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); }

Some more on Pre-defined Functions

A variety of "libraries"

  • iostream.h - for streams of character I/O
  • math.h - for some mathematical functions

— sqrt(double), pow(double, double), fabs(double)

  • stdlib.h - "standard library"

— abs(int), labs(long)

---------------------------------------

Type-changing ("casts")

int value; double result; value = 5; result = value/2; // ouch - stores 2.0 - not //what we intended // try result = double (value) / 2; // stores 2.

Parameter Passing

#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

  • units -> number cost -> price

Placing Calls

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 ; }

Scope

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); }

Local Variables

The concept of variable scope addresses this issue

  • variables declared in the function are only visible to that function: local variables
  • variables declared in the calling program are only visible to the calling program
  • unless the programmer wants them to be visible to the function

Global Variables

#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; ............................. }