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
(202009)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

January 14, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 14: Functions

    and Parameters in C Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 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
  3. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 3 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() . . . }
  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 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.
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 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.
  6. jgs More About Functions and Parameters

  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 jgs

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

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

    Passing String Address Using Pointer
  10. jgs Data Structures Linked List

  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 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; }
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 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; }
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 jgs

    Part 4 void print_list(struct node * head) { struct node * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } }
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 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; }
  15. jgs Data Structures Trees

  16. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 16 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); }
  17. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 17 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); } }
  18. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 18 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); } }
  19. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 19 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)); } } }
  20. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 20 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; }
  21. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    Spring 2018 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.