SlideShare a Scribd company logo
1 of 20
CSE240 – Introduction to
Programming Languages
Lecture 11:
Programming with C | Functions calls and parameter passing
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Part 1
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node * next;
};
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);
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
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;
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
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;
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Part 4
void print_list(struct node * head) {
struct node * current = head;
while (current != NULL) {
printf("%dn", current->val);
current = current->next;
}
}
Data Structures
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
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);
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
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);
}
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
traverse
void traverse(struct node *top) {
struct node *p = top;
if (p != 0) {
traverse(p->left);
printf("data = %dn", p->data);
traverse(p->right);
}
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
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));
}
}
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11
main
int main() {
struct node *root;
root=insert(root, 1);
root=insert(root, 2);
root=insert(root, 3);
traverse(root);
printf("%dn", search(root, 1) );
printf("%dn", search(root, 3) );
printf("%dn", search(root, 5) );
printf("%dn", search(root, 9) );
return 0;
}
Functions and Parameters
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13
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
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14
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()
. . .
}
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15
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.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16
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.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17
Call-by-alias and call-by-address
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18
Call-by-alias and call-by-address
Use the
C++ Compiler
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 19
Passing String Address Using Pointer
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
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.

More Related Content

What's hot

Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Dr. Loganathan R
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Hazrat Bilal
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringSri Harsha Pamu
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,pptAllNewTeach
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programmingBryceLohr
 

What's hot (20)

Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2Bcsl 033 data and file structures lab s2-2
Bcsl 033 data and file structures lab s2-2
 
Function basics
Function basicsFunction basics
Function basics
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 
Spiral array
Spiral arraySpiral array
Spiral array
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Mathematics Function in C ,ppt
Mathematics Function in C ,pptMathematics Function in C ,ppt
Mathematics Function in C ,ppt
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Lessons learned from functional programming
Lessons learned from functional programmingLessons learned from functional programming
Lessons learned from functional programming
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 

Similar to 201801 CSE240 Lecture 11

Similar to 201801 CSE240 Lecture 11 (20)

201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 07
201801 CSE240 Lecture 07201801 CSE240 Lecture 07
201801 CSE240 Lecture 07
 
201801 CSE240 Lecture 06
201801 CSE240 Lecture 06201801 CSE240 Lecture 06
201801 CSE240 Lecture 06
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
7 functions
7  functions7  functions
7 functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
4th unit full
4th unit full4th unit full
4th unit full
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»
 
Functions
FunctionsFunctions
Functions
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
Function in c
Function in cFunction in c
Function in c
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 05
201801 CSE240 Lecture 05201801 CSE240 Lecture 05
201801 CSE240 Lecture 05
 
201801 CSE240 Lecture 04
201801 CSE240 Lecture 04201801 CSE240 Lecture 04
201801 CSE240 Lecture 04
 
201801 CSE240 Lecture 03
201801 CSE240 Lecture 03201801 CSE240 Lecture 03
201801 CSE240 Lecture 03
 

Recently uploaded

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 

Recently uploaded (20)

Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 

201801 CSE240 Lecture 11

  • 1. CSE240 – Introduction to Programming Languages Lecture 11: Programming with C | Functions calls and parameter passing Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Part 1 #include <stdio.h> #include <stdlib.h> struct node { int val; struct node * next; }; 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); }
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 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; }
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 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; }
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Part 4 void print_list(struct node * head) { struct node * current = head; while (current != NULL) { printf("%dn", current->val); current = current->next; } }
  • 7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7 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); }
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 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); } }
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 traverse void traverse(struct node *top) { struct node *p = top; if (p != 0) { traverse(p->left); printf("data = %dn", p->data); traverse(p->right); } }
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 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)); } } }
  • 11. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 11 main int main() { struct node *root; root=insert(root, 1); root=insert(root, 2); root=insert(root, 3); traverse(root); printf("%dn", search(root, 1) ); printf("%dn", search(root, 3) ); printf("%dn", search(root, 5) ); printf("%dn", search(root, 9) ); return 0; }
  • 13. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 13 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
  • 14. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 14 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() . . . }
  • 15. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 15 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.
  • 16. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 16 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.
  • 17. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 17 Call-by-alias and call-by-address
  • 18. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 18 Call-by-alias and call-by-address Use the C++ Compiler
  • 19. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 19 Passing String Address Using Pointer
  • 20. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 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.