CS计算机代考程序代写 compiler Lecture 03

Lecture 03

Connect to slido.com
#ece209

ECE 209
Computer Systems Programming
Spring 2021

Lecture 07

Announcements
Z3 tomorrow, 2/18
Program 1 Friday, 2/26
Problem session PS2: 2/17 — CLion!!!
Quiz 2: 2/24

Photo by Patrick Fore on Unsplash

https://unsplash.com/@patrickian4?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText
https://unsplash.com/s/photos/announce?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText

New Data Types:
Arrays
Strings

Example

// array of char holding a string
// up to 9 chars
char name[10];

// finding string length
count = 0;
i = 0;
while (name[i]) {
count++;
i++;

}

Traversing a string.
Typically, will look for
null character instead
of knowing the length
of array.

Example
// finding string length
count = 0;
i = 0;
while (name[i]) {
count++;
i++;

}

// finding string length
count = 0;
for (i=0; name[i]; i++) {
count++;

}

// finding string length
count = 0;
while (name[i++]){
count++;

}

Common Mistakes

NULL is not the null character.
0 or ‘\0’ is the null character.

An array of int/double/… does not end with zero.
This is a property of string (data),
not true for arrays in general.

Functions

Building Blocks

LiteralsVariables

Operators

Expression

Statement

Function

Program

Unit of work

Unit of modularity

Values
(Data)

Source Code File Unit of compilation

Nouns

Verbs

Phrase

Statement

Paragraph

Document

Chapter

Function

You’ve seen functions before: subroutines.

call

return

In LC-3, this was
JSR and RET.

Modularity
Reusability
Abstraction

Function

A C function takes in arguments (values), and returns a value.

call

return

value, value, value,…

value

value = expression

Function

A function is a programmer-defined operator.

Arguments are its operands.

Return value is the result of the operation.

A function call is an expression!
z = x + y;

z = add(x,y);

Expression �
Literal

Variable

expr OP expr

OP expr

binary op

unary op

function call

Function Call

name(value,value,value,…)

A function call is an expression!

z = add(x,y);
if (f1(x-c) > 0)…
printf(“result = %d\n”, f2(x,y,z));
while (!finished())…

Function Call

name(value,value,value,…)

How many arguments?
What type of values?
What kind of value is returned?

Declaration

What does it do? Definition

Function Declaration

type name(type var,type var,…);

This is the type of
value returned by
the function.

Use void if there is
no return value.

Function Declaration

type name(type var,type var,…);

This is the type of
each argument:
each value that must be
passed to function.

A function may have zero arguments.

Function Declaration

type name(type var,type var,…);

This is an optional name
for each value.

The argument names are not needed,
and may be different than definition.
Provided to aid the reader.

Function Declaration

The declaration is all that is needed for the compiler
to process a function call.
name, number and types of arguments, return type

A function must be declared (or defined) before it is called.
If the compiler doesn’t know what the function looks like,
it can’t generate the correct code to call the function.

// This is a program.

#include

// function declarations
int myFun(int v1, int v2);
double area(double length, double width);

int main() {
double x, y;
scanf(“%d%d”, &x, &y);
printf(“area = %f\n”, area(x,y));
return 0;

}

Includes declarations
of standard library functions.

Standard Library Functions

double exp(double);
double pow(double x, double y);
double log(double);
double sin(double radians);
double sqrt(double);
double round(double);

int abs(int);
int rand();

When you call, don’t include types.

Standard Library Functions

int isalpha(int);
int isdigit(int);
int isupper(int);
int islower(int);
int ispunct(int);
int toupper(int);
int tolower(int);

When you call, don’t include types.

Function Definition

type name(type var,type var,…) {
statement…
// must return a value of the specified type

}

return value;

Return Statement

Function Definition

type name(type var,type var,…) {
statement…
// must return a value of the specified type

}

Must match the declaration.

Parameters: These are variables that are
initialized with values passed by caller.

Braces are required.

Return Statement
return value; Type of expression must match

the type of function.

If no return value (void), no expression.

Return statement can be anywhere, not just at the end.
Returns control immediately to caller.

There can be multiple return statements, but only one will be executed.

A void function can end without a return statement, if desired.

#include

// function declarations
double area(double length, double width);

int main() {
double x, y;
scanf(“%d%d”, &x, &y);
printf(“area = %f\n”, area(x,y));
return 0;

}

double area(double len, double w) {
return len * w;

}

I recommend putting
the definitions
after the main function.

Template #1

Lecture 07
Slide Number 2
Slide Number 3
Example
Example
Common Mistakes
Slide Number 7
Slide Number 8
Slide Number 9
Function
Function
Function
Slide Number 13
Function Call
Function Call
Function Declaration
Function Declaration
Function Declaration
Function Declaration
Slide Number 20
Standard Library Functions
Standard Library Functions
Function Definition
Function Definition
Return Statement
Slide Number 26
Slide Number 27