Looking for a Tutor Near You?

Post Learning Requirement »
x
x

Direction

x

Ask a Question

x

Hire a Tutor

PPT On Functions And Operators

Loading...

Published in: C / C++
1,281 Views

A study of these C++ ppts will help the student get an idea about the fundamental concepts in c++. Very useful for 11, 12th grade CBSE students who have taken c++ in computer science. This is an introduction to functions and operators.

Prerna S / Sharjah

4 years of teaching experience

Qualification: B.E in computer Science

Teaches: GRE, SAT, ACT, Maths, MS Office, Web Development, HTML, Java, English, Hindi, Science, Chemistry, Computer Science, Mathematics, English Language

Contact this Tutor
  1. Functions-introduction & Operators
  2. Funtion parameter ' A function parameter is a variable used in a function where the value is provided by the caller of the function. ' Function parameters are placed in between the parenthesis after the function identifier, with multiple parameters being separated by commas. ' int add(int x, int
  3. argument ' An argument is a value that is passed from the caller to the function when a function call is made. ' Eg ; printValue(6); // 6 is the argument passed to function printValue() ' add(2, 3);
  4. How parameters and arguments work together ' When a function is called, all of the parameters of the function are created as variables, and the value of each of the arguments is copied into the matching parameter. This process is called pass by value.
  5. //#include "stdafx.h" // Visual Studio users need to uncomment this line #include // This function has two integer parameters, one named x, and one named y // The values of x and y are passed in by the caller ' void printValues(int x, int y) cout
  6. practical ' function that adds two numbers together and returns the result to the caller.
  7. 2 10 //:tnclude iistdofx. h" // Visual Studio users need to uncontnent this line Sinclude int odd(tnt x, int y) return x y; return value- 9 int main() std: : cout return 0", add(4, 5) « std endl•,
  8. Keywords and naming identifiers ' C++ reserves a set of 84 words for its own use. These words are called keywords (or reserved words), and each of these keywords has a special meaning within the C++ language ' Eg goto inline int long mutable * namespace new noexcept not not_eq
  9. identifier ' The name of a variable, function, type, or other kind of object in C++ is called an identifier. There are a few rules that must be followed when naming identifiers: ' The identifier can not be a keyword. Keywords are reserved. ' The identifier can only be composed of letters (lower or upper case), numbers, and the underscore character. That means the name can not contain symbols (except the underscore) nor whitespace (spaces or tabs). ' The identifier must begin with a letter (lower or upper case) or an underscore. It can not start with a number. ' C++ distinguishes between lower and upper case letters. nvalue is different than nValue is different than NVALUE.
  10. m p les ' int value; // correct ' int Value; // incorrect (should start with lower case letter) ' int VALUE; // incorrect (should start with lower case letter) ' int VaLuE; // incorrect (see your psychiatrist) ;)
  11. ' If the variable or function name is multi-word, there are two common conventions: separated by underscores, or intercapped (sometimes called CamelCase, since the capital letters stick up like the humps on a camel). int my _ variable _ name; // correct (separated by underscores) ' void my _ function_name() // correct (separated by underscores) int myVariabIeName; // correct (intercapped/CameICase) ' void myFunctionName(); // correct (intercapped/CameICase) int my variable name; // invalid (whitespace not allowed) ' void my function name(); // invalid (whitespace not allowed) int MyVariabIeName; // valid but incorrect (should start with lower case letter) ' void MyFunctionName();
  12. local scope ' A variable's scope determines when a variable can be seen and used during the time it is instantiated. Function parameters and variables defined inside the function body both have local scope. That is, those variables can only be seen and used within the function that defines them. Local variables are created at the point of definition, and destroyed when they go out of scope (usually at the end of the function).
  13. #include int add(int x, int y) // x and y are created here // x and y are visible/usable within this function only return x + y; } // x and y go out of scope and are destroyed here int main() int a = 5; // a is created and initialized here int b = 6; // b is created and initialized here // a and b are usable within this function only std::cout
  14. following happens, in order: main() is executed 1. main's variable a is created and given value 5 2. main's variable b is created and given value 6 3. function add() is called with values 5 and 6 for arguments 4. add's variable x is created and given value 5 5. add's variable y is created and given value 6 6. operator + adds 5 and 6 to produce the value 11 7. add returns the value 11 to the caller (main) 8. add's x and y are destroyed 9. 10. main prints 11 to the console 11. main returns 0 to the operating system 12. main's a and b are destroyed
  15. operators Operators tell the expression how to combine one or more operands to produce a new result. ' #include ' int main() int x = 2; cout
  16. arithmetic operators ' addition (+), subtraction (-), multiplication (*), and division (/). Assignment (=) is an operator as well. Some operators use more than one symbol, such as the equality operator (==), which allows us to compare two values to see if they are equal.
  17. Operators come in three types: Unary operators act on one operand. An example of a unary operator is the - operator. In the expression -5, the - operator is only being applied to one operand (5) to produce a new value (-5). Binary operators act on two operands (known as left and right). An example of a binary operator is the + operator. In the expression 3 + 4, the + operator is working with a left operand (3) and a right operand (4) to produce a new value (7). Ternary operators act on three operands. There is only one of these in C++, which we'll cover later.
  18. forward declaration add.cpp int add(int x, int y) return x + y; main.cpp: #include int add(int x, int y); // forward declaration using function prototype int main() std::cout
  19. Header files Header files consist of two parts. The first part is called a header guard Header guards prevent a given header file from being #included more than once in the same file. The second part is the actual content of the .h file, which should be the declarations for all of the functions we want other files to be able to see. Our header files should all have a .h extension, so we'll call our new header file add.h:
  20. add.h: // This is start of the header guard. ADD_H can be any unique name. By convention, we use the name of the header file. #ifndef ADD H #define ADD H // This is the content of the oh file, which is where the declarations go int add(int x, int y); // function prototype for add.h -- don't forget the semicolon! // This is the end of the header guard #endif
  21. In order to use this header file in main.cpp, we have to include it. main.cpp that includes add.h: #include #include "add.h" int main() std::cout
  22. add.cpp int add(int x, int y) return x + y; Rule: Use angled brackets to include header files that come with the compiler. Use double quotes to include any other header files.
  23. C++ guarantees that the basic data types will have a minimum size: Category boolean bool character Type 1 byte char Minimum Size Note 1 byte May be signed or unsigned Always exactly 1 byte wchar_t 1 byte char 16 t char32 t 2 bytes C++ll type 4 bytes C++ll type integer short 2 bytes 2 bytes int long 4 bytes long long floating point 8 bytes C99/C++11 type float 4 bytes double 8 bytes long double 8 bytes