functions


 Function- is a subprogram that act on data and return a value.

Function Call by Value

Function Declaration – It is also called function prototype.  It tells the compiler the name, return type, and type of the parameters of the function.  The declaration of the function is called its prototype.

Function Definition – tells the compiler how the function works.

Syntax:           Return_Type    Function_Name(Argument Lists);

1.)    Function with void as return type and without argument list
Input
Process
Output
User-Defined Function
User-Defined Function
User-Defined Function

Example:         void highest();

2.)    Function with void as return type and with argument list
Input
Process
Output
main
Function
User-Defined Function
User-Defined Function

Example:         void highest(int num1, int num2);

3.)    Function with other data type as return type and without argument list
Input
Process
Output
User-Defined Function
User-Defined Function
main
 Function

Example:         int highest();

4.)    Function with other data type as return type and with argument list
Input
Process
Output
main
Function
User-Defined Function
main
 Function

Example:         int highest(int num1, int num2);



Sample Program: Create a C++ program that will input two numbers and output the highest number.  Implement this using function.


 























 















































 






















Function Call by Reference

Remember in the previous discussion that using function declaration void return type and with argument list that Input should be in main function, process and output should be in user defined function; Using Function Call by Reference Input is still in main, Process is in user defined function, but this time output is in the main function.

Note that in the following example, there is no Function declaration since the function definition is written before the main function.  In such case, no need to write the function declaration.

Example Number1:

#include

void byReference(int &num1,int &num2)
{
   num1 = num1 + 1;
   cout<< "num1 call by reference"<<
   num2 = num2 + 1;
   cout<<"num2 call by reference"<<
}

int main()
{ int num1, num2;

  num1 = 5;
  num2 = 6;

  cout<< "num1 before function call =  "<<
  cout<<"num2 before function =  "<<
 
  byReference(num1, num2);
 
  cout<< "num1 after function call =  "<<
  cout<<"num2 after function call =  "<<
  return 0;
}











Example Number2:


/*This program will also prompt the user to input two numbers and output he highest number. Notice that it uses function declaration 2 but the Output is in the main function.
*/
#include

void byReference(int num1,int num2, int &high)
{
  if(num1 > num2)
              high=num1;
  else
              high=num2;
}

int main()
{ int num1, num2, high=0;
 
  cout<<"Input two numbers: ";
  cin>>num1>>num2;
 
  byReference(num1, num2, high);
 
  cout<<"highest number = "<<
 
  return 0;
}














/* This is a program that will prompt the user to input ten numbers and output how many odd and even number were entered.  It uses function declaration with void as return type, and with three argument lists, first argument is the number entered, second argument is passed by reference, third argument is also passed by reference*/

#include

void oddeven(int num,int &ctrodd, int &ctreven)
{
   if (num % 2 == 0)
               ctreven++;
   else
               ctrodd++;
}

int main()
{ int num, ctr, ctreven=0, ctrodd=0;

  cout<<"Input ten numbers: "<
  for(ctr=1;ctr<=10;ctr++)
  { cin>>num;
    oddeven(num, ctrodd, ctreven);
  }
 
  cout<<"ctrodd =  "<<
  cout<<"ctreven =  "<<
  return 0;
}


















/* This program will input values for rows and columns to be able to produce a multiplication table.  Use function declaration with void as return type and two parameters row and column which is passed by value.*/


#include

void multtable(int row, int column)

{ int ans;

   for(int c=1;c<=column;c++)
               cout<<”\t”<
   cout<

   for(int r=1;r<=row;r++)
   {  cout<
               for(int c=1;c<=column;c++)
                        {  ans=r*c;
                                    cout<<”\t”<
                        }
   cout<
   }
}

int main()
{ int row, column;

  cout<<"Input rows and columns: "<
  cin>>row>>column;
  
    multtable(row, column);
  return 0;
}

Intro to Scilab

 
Intro to Scilab

Developed at INRIA, SCILAB has been developed for system control and signal processing applications. It is freely distributed in source code format.

High level scientific computing environments such as Matlab, RLab, Octave and Scilab are an enjoyable way to solve problems numerically.


It is particularly easy to generate some results, draw graphs to look at the interesting features, explore the problem further and manipulate matrices.

Scilab is made of 3 distinct parts

·         An interpreter
·         Libraries of functions
·         Libraries of Fortran and C routines


à - scilab prompt
Up arrow key – to display
// - comment
%pi = 3.1415 //constant pi
clc = clear the screen
clear = clear memory
clear varname = clear specific variable
xbasc() = erase the previous plot

Differences between Matlab and Scilab



à     (-1+2+3)*5 – 2/3  //The four arithmetic operations
= 19.33

à     2^3 // Means 2 to the power 3.
= 8

à     %pi // The mathematical constant pi
= 3.1415

à     exp(sin(%pi/2)) //The usual functions are provided log, log10, cos, tan, asin,…
= 2.7182818

exp – element-wise exponential

Calling sequence:
                        exp(X)

Parameters
·         X : scalar, vector or matrix with real or complex entries.

Description
                        exp(X) is the (element-wise) exponential of the entries of X.



à     %e //The mathematical constant e

Note:  Scilab does all calculations correct only to about 16 significant decimal digits.  This is enough accuracy for most purposes.  Usually only 5 significant digits are displayed on the screen.

Ex.

à  %pi
= 3.1415927

à 22/7   //pi is not the same as 22/7
= 3.1428571

à 11*(15/11)-15  //This shows there is round off error when scilab uses fractions.
= 1.776D-15

               
                à cos(%pi/3)  //These are familiar trigo functions
                = 0.5
                à sin(%pi/6)  //These are familiar trigo functions
                = 0.5

Variable – Scilab also uses variables to store intermediate answers.  A variable can have almost any name, but is must begin with a letter.
-          Scilab is a case-sensitive language.

Ex. 
X = 2+3
Y = 4+5
result = X/Y

; - echo off, suppress unwanted output.

Ex.
                à p = 2+3;
                à q= 3+5;
                à ratio = p/q
                = 0.5555556

>> A number of commands can be placed on the one line.  Use comma (,) or a semicolon (;)
Ex.
                à p1 = 2+3 ; q1 = x+4, ratio1 = p1/q1
                 
 q1 =
9
                ratio1 =
                                0.5555556

>>Parentheses can be used to make expressions clearer.
Ex.
                à ratio = (2+3)/(x+4)


Complex Numbers  -  Scilab handles complex numbers as easily as real numbers
                                                -  the variable %i stands for
Ex.
                à x=2+3*%i, y=1-1*%i
                x = 2 + 3i
                y = 1 – i

                à z1= x-y, z2 = x*y, z3 = x/y
                z1 = 1 + 4 i
                z2 = 5 + i
                z3 = -0.5 + 2.5i

                à abs(x)
                = 3.6055513

                à real(x)
                = 2

                à imag(x)
= 3         

                à sin(x)
                = 9.1544991 – 4.168907i

                à exp(%pi * %i ) + 1
                = 1.225D-16i












PLOTTING LINES AND DATA

-          This section shows how to produce simple plots of lines and data.

Ex.
Xk
.5
.7
.9
1.3
1.7
1.8
Yk
.1
.2
.75
1.5
2.1
2.4

à x=[.5  .7  .9  1.3  1.7  1.8]
à y=[.1  .2  .75  1.5  2.1  2.4]
à plot2d(x,y, style=-1)


Ex.
                Y1 = 2X + 4                           0 <= x1 <=2
                Y2 = x-2                                                3 <= x2 <=5

à x1 = [0: 0.5: 2];
à x2 = [3: 0.5: 5];
à y1 = (2*x1+4);
à y2 = (x2 – 2);
à plot (x1, y1)
à plot (x2, y2)
à plot (x1, y1, x2, y2)


MATRICES AND VECTORS

                Although SciLab is a useful calculator, its main power is that it gives a simple way of working with matrices and vectors.


Matrix Shortcuts

à a = eye(3,3)
à b = ones(4,4)
à c = diag([ 1 2 3 4])
à d = rand(3,3)
à e = [1:5]
à f = [1:3; 4:6; 7:9]
à g = [1:0.1:5]
à h = linspace(-10, 10, 20)
               
linspace
                syntax: linspace(x1, x2,n]

                where:
                                x1, x2 : real or complex scalars
                                n : integer (no of values) default = 100


Displaying matrix data


à a(1,1)
                = 1

à a(:, 1) // : is used to denote all entries
               =      1
                       4
                       7
à a(2, :)
               =      4      5      6

à a([1 2], [1 2])                                //a([r1, r2], [c1, c2])
               =      1      2
4         5

à a([2 3], : )
               =      4      5      6
                       7      8      9

à a([1 2],[2 3])
               =      2      3
5         6


MATRIX MANIPULATION

+             addition
-              subtraction
*             multiplication
/              division
\              left division
^             power
               transpose
.*            array multiply
./             array division
.^            array power

                 
Addition of Matrices                                                                      Subtraction of Matrices

A =                                                                                                         D = A – B
           1          2          3                                                                           D =
           4          5          6                                                                                  0         -1         2          
           7          8          9                                                                                  3         -2         2                                                              
                                                                                                                       4          3          2

           
B=
           1          3          1                                                                           E = B-A
           1          7          4                                                                           E =
           3          5          7                                                                                  0          1         -2                                                             
                                                                                                                      -3         2         -2             
C=A+B                                                                                                        -4        -3        -2             
C=
           2          5          4
           5        12       10
          10       13       16


F = 3*A                                                                                                 G = 2*B
F   =                                                                                                        G  =
           3          6          9                                                                                  2          6          2
          12       15       18                                                                                 2        14         8
          21       24       27                                                                                 6        10       14


H = A’                                                                                                    I = B’         
H  =                                                                                                        I =
           1          4          7                                                                                  1          1          3
           2          5          8                                                                                  3          7          5
           3          6          9                                                                                  1          4          7


Matrix Multiplication                                                                            Array Multiplication

                        A= [1 2 3; 4 5 6];                                             A = [2 4 6 8; 2 3 1 4]
                        B = [2 4 6 8; 1 2 3 4; 1 3 5 7]                                            B = [1 2 3 4; 2 3 1 4]

                        à C = A*B                                                               à C = A.*B
                                         C =                                                                                  C =
                                               7        17       27       37                                             2          8        18       32
                                              19       44       69       94                                             4          9          1        16

                        à C = B*A                            à C = A*B
                        ERROR! Inconsistent multiplication                          ERROR! Inconsistent multiplication 

                        à C = A.*B
                        ERROR! Inconsistent multiplication



           
                       
                       

                       
                                                       




Matrix Division                                                                                                 I

A = [2 4 6 8; 2 4 6 8; 1:4; 5:8]    
B = [1 5 1 4; 1 8 3 9; 1 7 9 2; 1 0 5 9]                                                            
                                                                                                                                           
C = A/B
C=
      1.6354          -0.7500          0.2604       0.8542
      1.6354          -0.7500          0.2604       0.8542
      0.8177          -0.3750          0.1302       0.4271
      6.8385          -3.8750          0.4010       1.6354


Inverse of Matrix

C = A*inv(B)
C=
      1.6354          -0.7500          0.2604       0.8542
      1.6354          -0.7500          0.2604       0.8542
      0.8177          -0.3750          0.1302       0.4271
      6.8385          -3.8750          0.4010       1.6354

Left Division

Given:
    -x1 + x2 + 2x3  = 2
    3x1- x2+ x3 = 6
    -x1 + 3x2 + 4x3 = 4

à A = [-1 1 2; 3 -1 1; -1 3 4]
à B = [2; 6;4]’
à X = inv(A)*B
X =
    1
    -1
    2

à A\B
X =
    1
    -1
    2