Page 6 - Modular_V2.0_C++_Flikpbook
P. 6

INSIDE THE SERIES



                  The key features of the series have been designed to ensure better learning, assessment
                  and evaluation.



                                                   Learning Resources









                                     Your Aim: This section                   Tech Funda: This section
                                    describes the objective of             provides a practical information
                                  3       the chapter.                         or tip to the students.
                                                 INPUT AND OUTPUT
                                                        IN C++
                                                                             statements is the statement block, that is to be repeatedly executed until the provided
                                                                            condition becomes false.
                                      Your Aim                                 Tech Funda
                                      to learn about:
                                           Output       Input                Do not put a semicolon (;) after the closing parenthesis of a for loop. If you do,
                                                                             the loop will keep running but will not execute any statements until the condition
                                            Escape Sequence Characters           Comments  becomes false.
                                 Every program requires some input to work and displays some output after processing the input.   Program 1: To print first 10 natural numbers.
                                 C++ also allows you to give input to a program and get the output from the program. Every
                                                                           #include<iostream.h>
                                 input/output in C++ is considered a stream of characters. C++ provides two objects to take the   DOSBox 0.74, Cpu speed: max 100%
                                  Declaration
                                 input and to display the output, which are cin and cout. Let us discuss these in detail.  #include<conio.h>  1 2 3 4 5 6 7 8 9 10
                                  Before using a function, you need to declare it. Declaring a function is called function prototyping.   void main()
                                   OUTPUT
                                  A function prototype tells the compiler about the data type and parameters of the function.  The   {
                                  syntax to declare a function in C++ is:
                                 The cout object is used to display output on a computer screen (monitor). It is part of the       int i;
                                 ostream class and is also known as the Standard Output Stream. The cout object works with       clrscr();
                                   return_type function_name([parameter 1, parameter 2, …]);
                                  Where, the return_type specifies the data type of the value returned by the function. If no value
                                 the stream insertion operator (<<) to show messages or values.
                                                                            Program 6: To print the alphabet Using a Function.
                                  is returned, the function should be declared with void. The function_name is the assigned name       for(i = 1; i <= 10; i++)
                                 For example:                                  {        DOSBox 0.74, Cpu speed: max 100% cycles, Frameskip 0, Program:
                                                                            #include <iostream.h>
                                  used to define and call the function later. Parameters are placed inside parentheses () and act as
                                   placeholders for input values. The declaration must end with a semicolon (;).          cout<< i << " ";   Alphabets from A to Z:
                                   cout<<"Output on the screen";
                                                                            #include <conio.h>
                                                                            void printAlphabets();
                                   int display(int);
                                 The above statement displays the message Output on the screen on the monitor. The stream       }  A B C D E F G H I J K L M N O P Q R S T U V
                                                                                       W X Y Z
                                                                            void main()
                                  In the above example, the display function is declared with int data type and and an int parameter.
                                 insertion operator can be used multiple times in a single statement to combine and display       getch();
                                                                            {
                                  Definition
                                 multiple outputs. For example:
                                                                           }     clrscr();
                                                                                cout<< “Alphabets from A to Z:\n”;
                                  After declaring a function, you need to define it. Defining a function means to give the body of
                                  cout<<"Hello"<<" from "<<" C++";         Using Comma (,) Operator with for Loop
                                                                                printAlphabets();
                                  the function. The definition of a function contains the functionality of the function in within curly
                                  cout<<x; Clickipedia: This section       C++ allows the use of the comma operator (,) to combine multiple expressions in a loop. When
                                 You can also print the variable value using the cout object. For example:
                                                                            Recap: This section provides
                                                                                getch();
                                  braces{ }. Let us consider the preceding example of display function:
                                                                            }
                                   int display(int age)                    two or more variables are used to control the loop, they are separated by the comma operator.
                                  cout<<120;                                void printAlphabets()
                                   {  provides interesting                 The comma operator allows: multiple initializations, multiple conditions and multiple counter
                                                                            {
                                 Chaining  insertions  is particularly  useful when  combining literals and  variables  in a  single   variables. a summary of the chapter for
                                       cout<<”Your age is: “ << age <<” years.”;
                                       return 0;
                                 statement. For example:                        char letter;
                                        computer facts.
                                                                                quick recapitulation.
                                                                                for (letter = ‘A’; letter <= ‘Z’; letter++)
                                   }
                                  cout<<"I am"<< name<<". I am"<<age<<" years old.";  Program 2: To use the comma operator in for loop.
                                                                                {
                                  In the preceding code, you can see that the return statement is used before the ending curly
                                                                                     cout<< letter << “ “;
                                 In the above statement, the values of the variables name and age are printed along with text,   #include<iostream.h>  DOSBox 0.74, Cpu speed: max
                                  brace of the function to return an integer value. Because, the return type of the function is int.
                                                                                }
                                 producing an output like:                 #include<conio.h>  i = 1, j = 10
                                  The return is jump statement in C++ that transfers the control to the calling function. If you do   i = 2, j = 9
                                                                            }
                                  not want to return any value from the function, then you need to declare a function with the void
                                  I am Chirag. I am 18 years old.          void main()        i = 3, j = 8
                                  data type. In this case, the function returns 0 (zero).  {  Recap   i = 4, j = 7
                                  24                                                          i = 5, j = 6
                                   Touchpad MODULAR (Ver. 2.0)-X               clrscr();
                                      Clickipedia
                                                                                              i = 6, j = 5
                                                                               A function is a block of organized and reusable code used to perform a particular task.
                                                                               int i, j;      i = 7, j = 4
                                                                               Built-in functions are predefined functions provided by C++.
                                                                               C++ allows you to create your own functions which are known as user-defined functions.
                                    The default return type of every function in C++ is int.       for(i = 1, j = 10; i <= 10; i++, j--)  i = 8, j = 3
                                                                                              i = 9, j = 2
                                                                               The parameters passed at the time of declaring a function are called formal parameters.
                                                                                              i = 10, j = 1
                                                                               {   There are two methods to call a function: call by value and call by reference.
                                                                               The scope of a variable means how different parts of the program can access that variable.
                                  Calling a Function                               cout << "i = " << i << ", j = " << j << endl;
                                  Calling a function means to use the function for performing the desired task. To call a function,
                                  you just need to write the name of the function with arguments, if any, in the parenthesis. The   60
                                  arguments are the actual parameter’s values. Generally, a function call occurred inside the main()   Touchpad MODULAR (Ver. 2.0)-X  Exercise
                                  function. However, you can call a function anywhere in the program.
                                                                            A.   Tick ( ) the correct option.
                                                                              1.  Which of the following is a block of code to perform a particular task?
                                                                              a.  Function      c.  Operator
                                  86                                           b.  Header file      d.  None of these
                                    Touchpad MODULAR (Ver. 2.0)-X
                                                                               2.  Predefined functions are called ……………………. functions.
                                                                               a.  Built-in      c.  User-defined
                                                                               b.  Both a and b      d.  None of these
                                                                                                     93
                                                                                                Functions in C++
                      Keyboard Shortcuts: This          Explore More: This section          Glossary: This section
                      section (in IV to X) contains    contains supplement topics for       contains definition of
                    keyboard shortcuts to perform           add-on knowledge.                important IT terms.
                         different operations.
                              Keyboard Shortcuts
                                 C++  Subject Enrichment
                            Shortcut Keys  Description
                           F2  To save a program
                           F3  To open a program
                           F10  To go to on the menu bar
                           Alt + Backspace  To undo the previous action
                           Shift + Alt + Backspace  To redo the previous undo
                           Shift + Del  To cut the code
                           Ctrl + Insert  To copy the code
                           Shift + Insert  To paste the code
                           Ctrl + Delete  To clear the screen
                           Ctrl + F9  To run a program
                           Alt + F9  To compile a program
                           Alt + Enter  Toggle screen mode (full screen/ window)
                           Alt + F7  To go to on the previous error
                           Alt + F8  To go to on the next error
                           Alt + X  To quit the Turbo C++ window
                                    Keyboard ShortcutsKeyboard Shortcuts 111  111
   1   2   3   4   5   6   7   8   9   10   11