Posts

Return By Value, Return By Pointer, Return By Reference

 Return By Value, Return By Pointer, Return By Reference Return by value: In this the function returns some kind of value like the variable, it is the most simplest way to return the value. remember it will return a copy of the variable, and it also doesn't affect the scope of the variable. int * myFunction() { . . . } Example: int doubleValue(int x) { int value = x * 2; return value; // A copy of value will be returned here } Return by Pointer/Address: return by pointer also known as return by address. in this type the address of the variable is returned. return by address can only return the address of a variable, not a literal or an expression (which don’t have addresses). if you try to return the address of a variable local to the function, your program will exhibit undefined behavior. Consider the following example: int* doubleValue(int x) { int value = x * 2; return &value; // return value by address here } in the above example the scope of the va

call by value, pointer, reference

 Call By Value, Pointer, References Here in this post, we have discussed the different parameter which we are pass in our user-defined function. The above all parameters are used for different situations. Call by value, call by reference, and call by pointer are three basic parameters which we often use in our function. Note : If we don't change our value and only we want to change its duplicate value then we only pass the call by value parameter in our functions. And if we want to change the original value of our data then we will pass by the pointer or pass by the references.   Call By Value Call By Reference While calling a function, we pass the copy of the values of variables to it. Such functions are known as “Call By Values”. While calling a function, instead of passing the values of variables, we pass the address of variables(location of variables) to the function known as “Call By References. In this method, the value of each variable in the calling function is copied into

Control Statements

 Control Statements (if, if- else, switch statement, loops)

Scope Resolution Operator

Scope Resolution Operator Scope Resolution Operator is used in C++. Scope Resolution Operator is used for different purposes for different reasons. There are many purposes like avoiding the name conflict, Access the global variable, and other different use. scope resolution operator is denoted by _::__. Now we will see one by one the different purposes of the scope resolution operator in oop. scope resolution operator to access global variables when the local and global variables have the same name. #include<iostream>   using namespace std;      int x;  // Global x      int main()  {    int x = 10; // Local x    cout << "The value of a global variable of the x is " << ::x;    cout << "\nThe value of a local variable of the x is " << x;      return 0;  }  Output : The value of a global variable of the x is 0. The value of a local variable of the x is 10. Scope Resolution Operator and Functions have some relation in oops. This operator

Implicit And Explicit Type Conversion

Image
Implicit And Explicit Type Conversion The type conversion is the conversion of a variable data type from one to another. suppose we are adding two numbers one integer and the other floating point number for Example:- 2 + 3.5, the answer will be 5.5, what we have done here is we know that 2 can also be written as 2.0 and then we perform the addition with 3.5 and we get the answer 5.5.  The same thing happens in the C++, the compiler knows that 2 which is integer and the 3.5 which is floating point number so it will automatically convert the 2 which is an integer type into float and then perform the addition.  This is known as implicit type conversion,  the implicit type conversion is the conversion that takes place automatically by the compiler. You might be thinking that why does the compiler not convert the number 3.5 in to floating point number? write. in the above example you might not have the question 🙋 , but in the following case you would definitely have the question, 2.0 + 1 i

Scope Resolution Operator

 Scope Resolution Operator Blocks and scopes are often used in the programming languages. we also know that the same variable names can be used to have different meaning in different blocks. the scope of the variable is from the point of the deceleration to the end of the block containing the deceleration, a variable declared in a block is said as local to that block. For example. ... { x = 10; ...    ... } ... ... { int x = 20; ... ... } In the above two blocks the x declared are two different variables at different memory location, we can not access the the variable outside the block or the other variable in side the different block. The Scope resolution operator is used for many other purpose, which are listed below: For accessing the global variable when there is a local variable with the same name.  Defining the function outside the class. Accessing a class’s static variables. Referring to a class inside another class. In case of multiple Inheritance. N

Reference Variable In C++

 Reference variable in C++ A reference variable is an alias to the existing variable, or it is just the other name to the variable. the reference variable points to the same memory address as the variable. any change done using the reference variable will change the original variable. we can use any of them to refer to the variable. To understand this concept think that a variable is labeled by the name, let's say 'i' in the memory location, the reference variable is just the second label to that memory location. so we can access the value stored in that variable using the original variable or the reference variable. for Example: int i = 17; int& reference = i; Here the & is not read as as address or ampersand it is read as reference. here is an example how the reference variable works. #include <iostream> using namespace std ; int main () { // declare simple variable int i ; // declare reference variable int & r = i ;