[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng Dept] / [R J Botting] / [CSci201] / 14
[Text Version] [Syllabus] [Schedule] [Glossary] [Labs] [Projects] [Resources] [Grading] [Contact] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] <14> [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Sat Feb 28 17:38:11 PST 2009

Contents


    14 Library and Other Functions

      Previous Logic etc

      [ 13.html ]

      Prepare

        6.1 Introduction

        6.2 Program Components in C++

        6.3 Math Library Functions

        Compiling with the cmath library

        If you use 'g++' to compile a program that includes the <cmath> library you will need to compile the program with an extra parameter "-lm" indicating the compiled math library must be linked in:
         		g++ -o example example.cpp -lm
        (l=link library, m=math).

        If you use 'Q' to do your comiplations it will do this for you.

        6.4 Function Definitions with Multiple Parameters

        6.5 Function Prototypes and Argument Coercion

        6.6 C++ Standard Library Header Files

      . . . . . . . . . ( end of section Prepare) <<Contents | End>>

      Deliver a question and P7

      Don't for P7 -- to get full credit it must be a program (even with errors) that includes a class based on your vision (even if not useful today).

      Can you change the value returned by a function

      If you store the returned value in a variable,
       		value = function (data);
      then you can do anything you want with it.

      What is an actual parameter and a formal parameter

      A function call contains the actual parameters:
       		function ( actual1, actual2, ...)
      The actual parameters can be expressions (normally...). They must give values of the right type to fit the formal parameters.

      The function definition has the formal parameters

       		type function (type formal1, type formal2, ...){...}
      Formal parameters are variables. They are never constants or expressions. They are place holders for the actual parameters.

      Why must I type double twice in function parameters

      In normal decalarations you may write
       		double x,y;
      and get two doubles. But in a function header
       		type foo(double x, y)
      is an error. I don't know why. But it is an error.

      By the way.... it is better style to have one variable per declaration as well!

      How do formal and actual prameters get matched up

      The formal and actual parameters are connected up one-to-one by the compiler. Each actual will be evaluated and passed to be the initial value of the formal parameter. The formal parameter (normally) then acts as a local varaible.

      Do the parameter types have to match precisely

      The compiler can convert numeric data as part of the matching process. This is called coercion because it is a forced conversion or cast.

      Thus the call

       		foo(1)
      will match all of the following
       		type foo(char c)...
       		type foo(int i)...
       		type foo(long l)...
       		type foo(double x)...
      I'm not sure about the rules when there are many matching parameters!

      If the actual is a string and the formal paramer is a string is it converted properly

      No.

      Suppose we have

       		void foo(int n){ cout << n+1 <<"\n"; }
      then
      		foo (string("124"));
      will not compile.

      More confusing is

       		foo ( "124" );
      which prints one more than the address where "124" has been stored, believe it or not. This is a historical accident.

      Why match one-to-one between actual and formal

      It works and is simple.... suppose we have
       		foo( 123, 456, 789);
      and
       		void foo( int x);
      how would you match the 3 actuals to one formal? And do you have the same idea as everybody else?

      Can a function change its formal parameter

      Yes.

      Can a function change the value of an actual parameter

      Only under special circumstance do the actions inside a function have any effect on the actual parameters given to it in a call. The rules are
      1. The formal parameter must be declared as a reference parameter [ 15.html ]
      2. The declaration/definition must therefore have an ampersand in it: type & variable.
      3. The actual parameter must be a variable of the same type.
      4. The call is just the same as any other call.
      5. The two variables (in the call and in the definition) are treated as a single variable.
      6. Changes to the formal parameter instantly update the actual parameter.

      More next time.

      Why doesn't cmath have any objects

      It predates OO programming (1970's).

      As a result you write

       		y = sin(x);
      not (Java):
       		y = Math.sin(x);
      I think this is convenient!

      As a rule, classes of objects are created in libraries when there is a clear consensus on an abstract type of data and how it behaves. Strings are an example, streams are an example. Later: vectors.

      As a rule, you should create classes for your project when they mirror real world object. In the ATM example: ATM, Account, Withdrawal, ... and so on. In the problem of grading: GradeBook...

      Does any C++ library have secant cosecant cotangent

      No. You have to do the division your self. You can always define your own function if you want:
       	inline double sec(double x) { return 1.0/cos(x); }

      Does the STL vary

      They are the same for each ditor you use. They may change from machine to machine, and will change operating system to operating system. They als change form time to time -- irritating but true.

      However they all tend to stay close to the standard... and that only changes ever 11 years or so.

      Is there a library that has a function that will find the minimum or maximum of an unknown number of numbers

      Not like this:
       		min( a1, a2, a3, a4, a5)

      However if the values are placed, or exist in a container then there is are functions in the STL which will search the container for the minimum and maximum elements:

       		min_element( vector.first(), vector.last() )
       		max_element( vector.first(), vector.last() )
      These are not in CS201.

      How can I find out what a function like ldexp is used and what it does

      I've found the following source [ ldexp.html ] for that function with links for many others.

      Explain function overloading

      You can give the same name to different functions as long as their parameters are of different types. When the compiler matches a function call with a function declaration it looks at the name of the function and the types of the actual parameters. It looks for a function with the same name and formal parameters of the same type.

      It is rather like the way that words in natural languages have different meanings depending on the context... TBA

      Only do this if the functions have similar purposes.

      Are the things in libraries global and what does this mean

      They are either global or private (hidden and unusable). Global things can be used any where in the program. Here are two example global variables from <iostream>
    1. std::cin
    2. std::cout

      <cmath> defines global functions, <string> defines global operations and the class string, etc.

      Why do I have to write a constructor

      Because it initializes variables. Uninitialized variables are bugs waiting to happen.

      It pays to think about how a new object is set up. A constructor describes this. The good news.... there are abbreviations for writing constructors that the book has not mentioned yet.

      How are global functions reused in other files

      The simplest technique is to put the whole definition in a file and "#include" it in the files that need to reuse the function.

      You are not required to do the following as part of CS201.


        A more complex but efficient technique (it reduces the compilation time...) is to split the functions file into a header file and a code file:
         		function.h
         		function.cpp

        The header file has just the function prototype

         		type function(formal parameters);

        The code file

         		#include "function.h"
         		type function(formal parameters) { body }

        You then pre-compile the code file:

         	g++ -c function.cpp
        which creates a "function.o" file.

        Somebody who want to use your function needs a copy of "function.o" and "function.h" and puts

         		#include "function.h"
        in their program. The must also put "function.o" in their compilations.

      You are not required to do this as part of CS201.

      Do you have to put a function in a header file

      No you can put it in the file that needs it -- but this only makes sense if it is only needed in that one file. Otherwise it pays to put it in a special file and use the above technique to include it where it is needed.

      Can you omit the int in unsigned long int etc

      Yes.

      When to use enum

      See ./15.html

      Did programmers invent viruses

      Only the electronic ones. This is one reason why computer science degrees include an ethics course. We have a responsibility to practice out skills and talents for the good not the bad.

      Exercises

      Next More on functions

      [ 15.html ]

      Quiz 6 on Logical operations and bool

    Abbreviations

  1. Algorithm::=A precise description of a series of steps to attain a goal, [ Algorithm ] (Wikipedia).
  2. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular and free C++ compiler.
  3. KDE::="Kommon Desktop Environment".
  4. OOP::="Object-Oriented Programming", Current paradigm for programming.
  5. Semantics::=Rules determining the meaning of correct statements in a language.
  6. SP::="Structured Programming", a previous paradigm for programming.
  7. Syntax::=The rules determining the correctness and structure of statements in a language, grammar.
  8. Q::software="A program I wrote to make software easier to develop",
  9. TBA::="To Be Announced", something I should do.
  10. TBD::="To Be Done", something you have to do.
  11. UML::="Unified Modeling Language".

End