Functions, Pointers, Structures

October 5, 2022
C++ Programming

Functions

Functions

Function, similar to the ones in math, is a collection of statements that is designed to perform specific tasks. Commonly, functions take in inputs (parameters), which is then processed to return an output. Below is the general format of functions in C++:

type name_of_function (parameters, parameters...){
statements
}
  • type: Type of variable the function will return
  • name_of_function: Name of the function that is used to call the function
  • parameters: Inputs that the funtion will use. Identify data type followed by variable name. Seperate with , for more than one input.
  • statements: Lines of code that will perform the function's task.

To call the function, you simply have to write the name of the function with the parenthesis following (if the function requires parameters, the parameters should be written between the parenthesis):

int squareNumbers(int x){ // Declares function "squareNumbers" that takes in parameter of x.    
y=x*x; //creates int variable equating to x squared    
return y; //returns the value of y when the function is called}
int main(){     input = 9;    
output = squareNumbers(input);    
//the function is called, resulting in the int variable "output" equating input squared
}

Pointers

Pointers are basically a reference to a variable in memory. They're "pointing" to a memory-address where a variable is stored.

Pointers in C++ are very similar to pointers in C. For more details about pointers, visit https://www.learn-c.org/en/Pointers.

C++, however, adds some more features, namely the operators new and delete to allocate and de-allocate memory to a pointer, respectively.

datatype

A pointer in almost all cases needs a datatype that matches the referenced variable. For example an int-pointer can hold the address of an int-variable and a float-pointer can hold the address of a float-variable. A special case is the so called "void-pointer".

void pointer

A so called "void-pointer" is a pointer, which has no data type assigned. It can hold an address of any type and can be typecasted to any other type.

void pointer in C++ particularity

One of the biggest differences is that pointers in C++ are more secure. In C, we can assign a void pointer to a non-void pointer without using a cast to non-void pointer. In C++, however, you have to explicitly type cast it.

void* p1;
char* p2;
p2 = p1; // Valid in C but not C++
p2 = (char*) p1; // In C++ we have to typecast the pointer!

Structures

What is a structure

If you start programming code in C++ you'll sooner or later be facing a situation in which you'll need to store coherent data in a structured way. This is where structures come in place...

A structure is basically a user-defined datatype which consists of other datatypes like int, char, etc. ...

Defining a structure

//This is how you'll usually define a structure    
using namespace std;                                                                                                                                                           
struct MyOwnStructure { };
int property_one;    
int property_two;    
char property_three;    
bool property_four;

Using a structure

int main (void) {
MyOwnStructure demo1; //Declare demo1 of type MyOwnStructure    
MyOwnStructure demo2; //Declare demo2 of type MyOwnStructure  
//set demo1's with values    
demo1.property_one = 1;    
demo1.property_two = 2;    
demo1.property_three = 'a';    
demo1.property_four = false;    
//set demo2's values    
demo2.property_one = 3;    
demo2.property_two = 4;    
demo2.property_three = 'b';    
demo2.property_four = true;    
cout << "Demo1: " << demo1.property_one << demo1.property_two << demo1.property_three << demo1.property_four << endl;    
cout << "Demo2: " << demo2.property_one << demo2.property_two << demo2.property_three << demo2.property_four << endl;  
return 0;
}
VIkas Donta

My name is VIkas Donta and I first discovered Web Designingin 2018. Since then, It has impact on my web design projects development career, and  improve my understanding of HTML/CSS tremendously!

Related Posts

Stay in Touch

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form