Coding - C

Functions, Variable Scope, and Memory

네레이션 2022. 5. 20. 22:23

⭐️ Functions: Mixture of program codes to execute to meet a single specific purpose.

⭐️ Reason for using functions: To avoid repetitive programming, if made, then can be called for later use. 

If the code is divided to multiple parts, due to modular coding, the code becomes easier to read. It is also easier to fix or modify code when it is divided to functions.

 

Basic Format of function: Return type, Name of Function, Parameters, Command

int name (x,y) {

    command

}

 

Return type: The type of data that the code is going to return. (Ex. char, int, double, str, etc.)

Name of function: Can be called for later use.

Parameters: The variables that save the numbers that will be used in the parameter. It is something that connects the                          thing that builds the function (Parameter) and the thing that call and uses those functions. (Numbers)

Command: The code that is run to meet the purpose of the function.

 

The bigNum is the name, the num01 and num02 are the parameters, and 3, 5, 1, and 7 are the numbers that go in the parameter.

http://www.tcpschool.com/c/c_function_basic

 

Order of functions and Prototype

Order of functions are very important; in this code, it prints error because the function "bigNum" is out of scope. (The computer doesn't know what bigNum is when the int main is ran.)

http://www.tcpschool.com/c/c_function_basic

In this case, we use Prototype, which tells that the function is defined later in the code. 

The basic format of Prototype is returnType nameFunc (paraType). The return type and parameter types are both integers, name of function is bigNum. This won't return a error because we told the computer that the function bigNum is defined later in the code. 

 

 

 

Variable Scope: Divided to Local Variable, Global Variable, Static Variable, and Register Variable

Local Variable: Variable that has been declared in the block. It is deleted when the block ends. Initializing the value is required, or it will give a random trash value.

 

The void local(void) is the local variable, and the values of var are 10, 30, 10, and 20 in order. In the if statement, the local variable var is overwritten to 30, only in the if statement block. So when the if statement ends, the var would return to 10 again. Same thing with void(local), var is overwritten by 20 only in the void(local) block. 

http://www.tcpschool.com/c/c_function_variableScope

Global Variable: Variable that is declared outside of the function. It is accessible anywhere in the code, and will be deleted only when the program ends. Initializing is not neccesary as the value is automatically initialized to 0.

 

The global variable car is declared, and the value of var is 0, 10, 20, 10 in order. The global variable var is accessible anywhere until the local variable is declared in main(void) function, since the local variable var overwrites the global variable var. 

http://www.tcpschool.com/c/c_function_variableScope

Static Variable: Variable that has been declared with "static". It possesses both characteristic of global variable and local variable. It is initialized only once at the start of code, and will be deleted only when the program ends.

 

The local variable count will only give 1s, as the data is deleted each time the program is newly called. However, the static variable static_count will still have the data, counting 1, 2, 3.... The static_count is initialized to 1 only once, and isn't accessible except for the staticVar function.

Register Variable: Variable that has been declared with "register", it is stored in CPU's register memory, so it is easily accessible. But since this is so small, the C program just declaress this as local variable. 

 

 

 

Memory

Memory is divded innto three categories: CPU, Disc, RAM

CPU: They are only for calculation so it is really fast, and they are unable to remember or do multiple things at once.

Disc: They are able to remember data very long, and they are very slow at calculation.

RAM: They are slower than CPU at calculation, but able to remember many things at once.

 

Four types of memory area that they receive from OS: Code Area, Data Area, Stack Area, and Heap Area.

http://www.tcpschool.com/c/c_memory_structure

Code Area: Where the code we write is saved in the memory. The CPU takes the code and solve one by one.

Data Area: Where the Static Variables and Global Variables are saved. The area is created with the start of program                       and deleted when the program ends. The computer doesn't forget this data, as those variables are needed                     through the entire part of code. 

Stack Area: Where the Local Variables and Parameters are saved. The area is created with the declare/call of the                             function and will be deleted when the called function has ended. The data of the function are stack frame. 

Heap Area: This is the Memory Area that the programmer "can and has to" manage. The area given is determined by                        the programmer. We will learn about this later. 

 

⭐️ Reason for dividing Variable Scopes to Static, Global, Local, etc: To be more efficient with the code, for usage with different purposes, 용도에 맞게 사용하도록, for better application of the code.