Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 14

CSE240 Lecture 14

Introduction to Programming Languages
Functions and Parameters in C
(202110)

Javier Gonzalez-Sanchez

January 14, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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
  2. 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%
  3. 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
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

    Part 1 #include <stdio.h> #include <stdlib.h> 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; }
  5. 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; }
  6. 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; }
  7. 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; } }
  8. 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
  9. 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() . . . }
  10. 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.
  11. 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.
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 jgs

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

    Passing String Address Using Pointer
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 jgs

    node #include <stdio.h> #include <stdlib.h> 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); }
  15. 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); } }
  16. 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); } }
  17. 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)); } } }
  18. 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; }
  19. 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.