Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 14: Functions and Parameters in C Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs Announcements § It opens today and it is due on October 19 (before our lecture) § All about Language C Note: § By next week you will have 3 quizzes and 3 assignments graded. They are 25% of your final grade. § Midterm exam is another 25%

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 jgs Announcements § Midterm Exam, Thursday October 21, during the lecture time. Closed-book, comprehensive exam, on paper (only pen/pencil and eraser needed). § Midterm review, Tuesday October 19

Slide 4

Slide 4 text

jgs Data Structures Linked List

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs Part 1 #include #include struct node { int val; struct node * next; }; // copy here the functions from // the other slides. // Yes. Here – before main int main() { struct node * head = NULL; head = malloc(sizeof(struct node )); head->val = 1; head->next = malloc(sizeof(struct node )); head->next->val = 2; head->next->next = NULL; push (head, 3); push (head, 4); push (head, 5); print_list(head); printf("-----\n"); pop(&head); pop(&head); pop(&head); print_list(head); return 0; }

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs Part 2 void push(struct node * head, int val) { struct node * current = head; while (current->next != NULL) { current = current->next; } current->next = malloc(sizeof(struct node )); current->next->val = val; current->next->next = NULL; }

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs Part 3 int pop(struct node ** head) { int retval = -1; struct node * next_node = NULL; if (*head == NULL) { return -1; } next_node = (*head)->next; retval = (*head)->val; free(*head); *head = next_node; return retval; }

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 jgs Part 4 void print_list(struct node * head) { struct node * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } }

Slide 9

Slide 9 text

jgs More About Functions and Parameters

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs Scope of Declaration §Scope Rule: The scope of a variable starts from its declaration point and extends to the end of the current block in a pair of braces. §Declaration-before-use: Variables and functions must be declared before they are used. { int height = 6; int width = 6; int area = height * width; // . . . } // block ends

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 jgs Scope Rule: Forward Declaration void bar(void); // forward declaration to satisfy scope rule int foo(void); // forward declare all functions int foo() { // genuine declaration . . . bar(); // call function bar() . . . } void bar() { // genuine declaration . . . k = foo() // call function foo() . . . }

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs Functions and Parameter Passing § A function is a named block of code that must be explicitly called. § The purposes of using functions are twofold: abstraction and reuse: abstraction: statements that form a conceptual unit; reuse: statements that can be executed in more than one place in the program. § Functions communicate with the rest of the program by using either global variables or parameters / return value § Formal and actual parameters. In the declaration formal parameters. When calling a function, actual parameters are given.

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs Parameter Passing § Call-by-value: a formal parameter is a local variable in the function. It is initialized to the value of the actual parameter. It is a copy of the actual parameter. Advantage: no side-effects (safe, reliable). Drawback: less flexible/less powerful. § Call-by-address (pointer or reference): the formal parameter is a pointer to the actual parameter. There is only one variable with two names. Changing the formal parameter immediately changes the actual parameter. Reference in C++ only. Drawback : side-effects (programmer skills) Advantage: flexible/powerful.

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 jgs Call-by-alias and call-by-address

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 jgs Call-by-alias and call-by-address Use the C++ Compiler

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 jgs Passing String Address Using Pointer

Slide 17

Slide 17 text

jgs Test Yourselves Trees

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 jgs node #include #include struct node { int data; struct node* left; struct node* right; }; struct node* newNode(int data) { struct node* node = malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); }

Slide 19

Slide 19 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 jgs insert struct node* insert(struct node* node, int data) { if (node == NULL) { return(newNode(data)); } else { if (data <= node->data) node->left = insert(node->left, data); else node->right = insert(node->right, data); return(node); } }

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 jgs traverse void traverse(struct node *top) { struct node *p = top; if (p != 0) { traverse(p->left); printf("data = %d\n", p->data); traverse(p->right); } }

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 21 jgs search int search(struct node* node, int target) { if (node == NULL) { return 0; } else { if (target == node->data) return 1; else { if (target < node->data) return(search(node->left, target)); else return(search(node->right, target)); } } }

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 22 jgs main int main() { struct node *root; root=insert(root, 1); root=insert(root, 2); root=insert(root, 3); traverse(root); printf("%d\n", search(root, 1) ); printf("%d\n", search(root, 3) ); printf("%d\n", search(root, 5) ); printf("%d\n", search(root, 9) ); return 0; }

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 23 jgs Questions

Slide 24

Slide 24 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.