Function & Variables (20241015) - 01 PDF
Document Details
Tags
Summary
This document provides a lecture on the concepts of functions and variables, including local and global variables in C programming. It explains how variables are declared and used, discussing aspects like variable types and scope along with the lifetimes of variables.
Full Transcript
CH. 9 FUNCTION & VARIABLES Properties of Variables Properties of Variables : Name, type, size, value + scope, lifetime, linkage scope : Available range of variables Lifetime : How long the variable exists in memory linkage : Connection status with variables in other areas...
CH. 9 FUNCTION & VARIABLES Properties of Variables Properties of Variables : Name, type, size, value + scope, lifetime, linkage scope : Available range of variables Lifetime : How long the variable exists in memory linkage : Connection status with variables in other areas Scope of Variable Scope of Variable Global variable Local variable Function1 Global variable Function2 Local variable Local variable Global variable & Local variable Global variable : defined outside of a function Local variable : defined inside of a function Local variable Variables declared within a block Usable range of x Usable range of y An error occurred because y left the block in which it was declared. Where are local variables declared? In newer versions of C, declarations can be made anywhere within a block!! Local variables can be declared even in the middle of a block Local variables with the same name If the blocks are different, we can use variables with the same name. However, it is better to avoid using duplicate names if possible. Lifetime of local variables Block Creation When the block in which a local variable is declared ends, it is Lifetime automatically expired. Expiration Example #1. (Lifetime of local variables) #include int main(void) { int i; Each time a new block for(i = 0;i < 5; i++) is started, it is created { and initialized again. int temp = 1; printf("temp = %d\n", temp); temp++; } return 0; } temp = 1 temp = 1 temp = 1 temp = 1 temp = 1 Initial value of local variable #include Since it has not int main(void) been initialized, it { has a remaining int temp; value(garbage). printf("temp = %d\n", temp); return 0; } Garbage is a value left in memory. Parameters of the function Parameters are also kind of like local variables. Parameters are initialized with the arguments delivered when calling. Parameter and local variable of int inc(int counter) function inc { counter++; return counter; } Example #2. (Parameters of the function) #include int inc(int counter); int main(void) { int i; Call by value i = 10; printf(“Before calling the function i=%d\n", i); inc(i); Parameters are also printf(" After calling the function i=%d\n", i); a type of local variable. return 0; } void inc(int counter) { counter++; Before calling the function i=10 } After calling the function i=10 Global variable global variable : is a variable declared outside of a function. The scope of use of global variables is the entire source file. Function1 Global variable Function2 Local variable Local variable Initial values and lifetime of global variables #include int A; int B; int add() { return A + B; } int main(void) Scope of { The initial value of global int answer; global variables is 0 variables A = 5; B = 7; answer = add(); printf(“ % d + % d = % d\n”, A, B, answer); return 0; } 5 + 7 = 12 Initial value of global variable #include int counter; Global variables are initialized to 0 when the compiler runs the int main(void) program. { printf("counter = % d\n", counter); return 0; } counter = 0 Use of global variables #include Let's predict int x; what the output void sub(); will be. int main(void) { for (x = 0; x < 4; x++) sub(); } void sub() { for (x = 0; x < 4; x++) **** printf("*"); or **** } **** **** **** Tips of using global variables Let’s declare common data used in almost all functions as global variables. Declare data that is used only by a few functions as local variables. (If necessary, they can be connected to each other through delivered arguments and parameters.) Global and local variables with the same name - no problem grammatically. #include int sum = 1; // Global If possible, avoid using it like this. int main(void) { int sum = 0; // Local printf("sum = %d\n", sum); return 0; } sum = 0 Lifetime(means survival time in memory) static allocation : It continues to survive while the program is running. automatic allocation : Created when entering a block Expired when leaving a block Lifetime Factors that determine lifetime Location where variable is declared storage type specifier Storage type specifier auto register static extern haven't learned about it yet. Storage type specifier - auto It is automatically created at the location where the variable is declared, and is automatically expired when getting out of that block. (auto) can be omitted for local variables. int main(void) { In principle, auto int sum = 0; auto int sum = 0; int i = 0; However, auto can be... omitted in local variable, so... can be written like } int sum = 0; Storage type specifier - static #include void sub() { If we add static to a local variable, the local variable becomes a static static int scount = 0; variable. int acount = 0; printf("scount = %d\t", scount); As a result, lifetime becomes longer. printf("acount = %d\n", acount); scount++; acount++; } int main(void) { sub(); sub(); scount = 0 acount = 0 sub(); scount = 1 acount = 0 return 0; scount = 2 acount = 0 } Storage type specifier - register Store variables in registers. register int i; for(i = 0;i < 100; i++) sum += i; Variables are stored in registers in the CPU Lab 1: Implementing a bank account Let's make a printout form for the bank account. Use a user-defined function save(int amount). This function receives the amount to be saved from main() and outputs the deposit amount, withdrawal amount, and balance as shown in the following screen.(Deposits and withdrawals total 4 times) ============================== Deposit withdraw balance ============================== 10000 10000 50000 60000 10000 50000 30000 80000 ============================== Lab 2: Printing the number of dice faces Build a program that throws a dice a total of 100 times and prints the number of times each side came up. Create a function get_dice() that throws a dice, and print the number of times for each side within this function, as follows.(Use rand() and else if statement) 1->12 2->21 3->17 4->16 5->14 6->20 Lab 3: Printing the number of dice faces Upgrade the results of LAB 2 so that the results are different every time. 1->12 2->21 3->17 4->16 5->14 6->20 Q&A