Essential C++ # 02. Procedural Programming

C++

Chapter 2. Procedural Programming

📌Definition of Function

It is composed of:

1️⃣ the return type of a function.

2️⃣ the name of the function.

3️⃣ the parameter list of the function.

4️⃣ the body of the function.

 

📌The & in front of a parameter in a function

The & in front of a variable is to take its address.

The & in front of a parameter is to indicate that modifying the original:

Apparently, the IncrementA does increment the number, because it modifies the origin by using &.

 

📌& in C++ and ref in C#

& in a function declaration is very much the same as ref in a function declaration.

Apparently, IncrementA does increment.

 

📌Things behind invoking a function

When we invoke a function, a special area of memory is set up on stack. Within this special area of memory there is a space to hold the value of each function parameter and variables inside the scope(local variables).

When the function completes, this area of memory is discarded.(popped from the program stack)

By default, an object is passed to a function, its value is copied to the local definition. That is called pass by value.

If we want to modify the original value, it is called pass by reference.

📌Can't return pointer/reference inside the stack

Due to the preceding mechanism, returning the address of one of these local objects results in serious run-time program errors❌.

However, if you do want to return something inside the stack. You have to declare it as static. This is very special in C++(compared to C#).

 

 

📌MAIN DIFFERENCE in pass by reference between C++ and C#!!⭐⚠

In C#:

The preceding 2 declarations are the same. Because in C#, List<T> is passed by reference by default.

 

In C++:

The preceding 2 declarations are different. Because in C++, everything is passed by value by default. You have to explicitly assure whether is passed by reference or passed by value.

 

📌Pass by Reference Semantics

The & mark has 3️⃣ meaning and usage:

1️⃣: retrieve the address of a variable

2️⃣: indicate a function is pass by reference

3️⃣: Set up a reference between objects.

 

 

📌Benefit Using Pass by Reference🌟

The following 3 functions do the same thing which merely cout the value. But he difference is that the former one is much FASTER.

The const keyword indicates that the function will not modify the input.🌟

 

📌Difference between reference parameter and pointer parameter

Before we dereference a pointer, we MUST always make sure the pointer is not set to 0.

A reference always refers to some objects and therefore the check for 0 is unnecessary.

 

📌👍Lippman's advice on passing by reference

"I recommend not passing built-in types by reference. The reference mechanism is primarily intended to support the passing of class objects as parameters to functions."

 

 

📌What is Storage Duration(extent)?

It refers to the period of time for which memory is allocated for an object is called its storage duration or extent.

 

📌Categories of Extent

There are 3 extents:

1️⃣Local extent

  • easy to understand, e.g. the local variable
  • managed automatically

2️⃣File extent(static extent)

  • the variables in file scope, which are outside of main() function. Its memory is allocated before the beginning of main() and remains allocated until the program is terminated.
  • managed automatically

3️⃣Dynamic extent

  • The memory comes from the heap.
  • managed by programmer explicitly with new and delete.

 

📌Example of Dynamic Memory Allocation

The syntax is:

Declare, but not initialized:

Declare, also initialized:

Allocate an array of heap elements:

Delete an object:

Delete an array:

 

📌Dangerous! Memory Leak⚠

If you don't remember to delete something, in the end it will cause memory leak.

 

📌Lippman' advice on designing a function👍

"It is better to communicate between functions using parameters rather than use objects defined at file scope. Since function dependent on file scope is harder to reuse in a different context."

 

📌inline Functions

  • What is it?

An inline function represents a request to the compiler to expand the function at each call point. With an inline function, the compiler replaces the function call with a copy of the code to be executed.

 

  • Why use it?

To reduce the function call overhead[^1].

 

  • When to use it?

inline function only suits to small function.

 

  • What is its analogy?

The analogy of C++ inline function in C# are Action<> and Func<>.

 

 

📌Template Functions

 

 

📌Pointers to Functions

God damn it. This is really really hard...

//TODO well explained next time...

 

📌Header Files

  • use ".h" as suffix by convention
  • ok to multiple declarations
  • only one definition of a function in a program
  • usually no definition in header files
  • inline functions definition and template class definition should be in header files

 

📌What is extern?

Suppose we have something in the header file:

It is not right... Since the seq_cnt as 6 will be passed in the function declaration. While the declaration is merely declaration. Therefore we could use:

The extern means the following is just declaration, please ignore the parameter name. It does not have any relationships with others.

 

📌Different include

You might see:

The short answer is:

Use " ", if the header file is in the same directory as the program text file including it.

Use < >, if the header file is anywhere else.

 

 

Previous
Previous

Essential C++ # 03. Generic Programming

Next
Next

Essential C++ # 01. Basic C++ Programming