Professional Documents
Culture Documents
Embedded Programming
Chapter 3: Functions
and Libraries
Lecturer: Dr. Hoang Duc Chinh (Hoàng Đức Chính)
Department of Industrial Automation
School of Electrical Engineering
Email: chinh.hoangduc@hust.edu.vn © HDC 2021.1
Content
3.1 Functions and Functional programming
3.2 Function declaration and definition
3.3 Arguments and returning values
3.4 Function design and library
3.5 ANSI-C standard library
3.6 Working with files in C/C++
3.7 Function overloading in C++
3.8 Inline function in C++
Data-oriented programming
D_1
Data 1
D_2
Data 2
D_3
Data 3
int x = 5;
int k = SumInt(x,10); Returning value
(no name)
...
Actual
parameters
SumInt
int a = 2; x a
k
k = SumInt(a,x); 10 b
Variable assigned
to returning value
Arguments
Chapter 3: Functions and Libraries © HDC 2021.1 21
3.3.1 Passing values
int SumInt(int,int);
// Function call
void main() {
int x = 5; SP
b = 10
int k = SumInt(x,10);
a=5
... SP
} k = 45
x=5
// Function definition
int SumInt(int a,int b) {
...
https://devdocs.io/cpp/io/c
char pet[5];
Equavalent
pet[0] = ‘l’; pet[1] = ‘a’; pet[2] = ‘m’;
pet[3] = ‘b’; pet[4] = ‘\0’;
Do not use:
char pet[5];
pet = “lamb”; /* No array assignment in C */
small: Bob\0?
strcpy(str3, str1);
strcat(str3, str2);
/* strcat(s1, s2)
make a copy of s2 at the end of s1. */
str3 ? ? ? ? ? ? ? ? ? ? ?
str3 l a m b \0 ? ? ? ? ? ?
str3 l a m b c h o p \0 ? ?
compiler compiler
ANSI lib
linker
other libs
.exe file
Chapter 3: Functions and Libraries © HDC 2021.1 84
Library
Files consist of functions which have been built and
compiled
Reduce system dependency
Reuse existing source code
Enhance portability Standard
ANSI C Libraries
scanf(“%d”, &score);
A…
while (score != 0) {
printf(“%d \n”, score);
scanf(“%d”, &score);
}
printf(“File copied.\n”);
fclose(infilep);
fclose(outfilep);
void main() {
int k = max(5,7); // call (1)
double d = max(5.0,7.0); // call (2)
double a[] = {1,2,3,4,5,6};
d = max(d, a[1], a[2]); // call (3)
d = max(a, 5); // call (4)
d = max(5,7); // ?
d = max(d, 5); // ?
}