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
PRO

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

    View Slide

  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%

    View Slide

  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

    View Slide

  4. jgs
    Data Structures
    Linked List

    View Slide

  5. 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;
    }

    View Slide

  6. 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;
    }

    View Slide

  7. 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;
    }

    View Slide

  8. 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;
    }
    }

    View Slide

  9. jgs
    More About
    Functions and Parameters

    View Slide

  10. 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

    View Slide

  11. 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()
    . . .
    }

    View Slide

  12. 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.

    View Slide

  13. 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.

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. jgs
    Test Yourselves
    Trees

    View Slide

  18. 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);
    }

    View Slide

  19. 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);
    }
    }

    View Slide

  20. 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);
    }
    }

    View Slide

  21. 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));
    }
    }
    }

    View Slide

  22. 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;
    }

    View Slide

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

    View Slide

  24. 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.

    View Slide