UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming1 Functions (2)

Slides:



Advertisements
Similar presentations
Week 8 Arrays Part 2 String & Pointer
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
1 Passing Array Array’s element can be passed individually to a function; copying value exist during passing process. An entire array can be passed to.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
Functions Pass by Value Pass by Reference IC 210.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
1 Structures UniMAP SEM I - 11/12EKT 120 Computer Programming.
Functions Pass by reference, or Call by reference Passing addresses Use of & part 3.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
How to design and code functions Chapter 4 (ctd).
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
User-Defined Functions (cont’d) - Reference Parameters.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
EKT120: Computer Programming
Chapter 7: User-Defined Functions II
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
C Functions -Continue…-.
EKT120: Computer Programming
Week 6 – Functions (2) PGT C Programming.
A Lecture for the c++ Course
Programming Fundamentals Lecture #7 Functions
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
EKT120: Computer Programming
EKT120: Computer Programming
DKT121:Fundamental of Computer Programming
Functions in C Mrs. Chitra M. Gaikwad.
Scope, Parameter Passing, Storage Specifiers
Variables and Their scope
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions Pass By Value Pass by Reference
Simulating Reference Parameters in C
Function “Inputs and Outputs”
Chapter 7: User-Defined Functions II
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Functions.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
C Parameter Passing.
Presentation transcript:

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming1 Functions (2)

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming2 Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs. local variable Pass by value

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming3 Sample application Write a C program that reads item code and quantity, then calculate the payment.Use functions: menu – print item code menu determine_price – determine price based on item code calc - calculate payment print_result – print payment Think!! Which function return no value and which function return a value. What argument name do I want to feed in as parameters and what to return??

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming4 Sample application-cont #include void menu(); float determine_price(int); float calc(float,int); void print_result(float); int main() {int code,qty;float price,pay; menu(); printf("Enter item code and quantity:"); scanf("%d %d", &code,&qty); price= determine_price(code); pay=calc(price,qty); print_result(pay); return 0; }

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming5 Sample application-cont void menu( ) { printf("Code\tItem\tPrice\n"); printf("1\tPapaya\t1.00\n"); printf("2\tMelon\t2.00\n"); printf("3\tDurian\t3.00\n"); printf("\tOthers\t4.00\n"); } float determine_price(int item_code) {float pricing; switch(item_code) { case 1:pricing=1.00;break; case 2:pricing=2.00;break; case 3:pricing=3.00;break; default:pricing=4.00; } return(pricing); } float calc(float item_price,int quantity) {float answer; answer=item_price*quantity; return(answer); } void print_result(float payment) {printf("Payment is %.2f\n", payment); } week5]$ gcc testing.c week5]$./a.out Code Item Price 1 Papaya Melon Durian 3.00 Others 4.00 Enter item code and quantity:1 3 Payment is 3.00 week5]$./a.out Code Item Price 1 Papaya Melon Durian 3.00 Others 4.00 Enter item code and quantity:9 3 Payment is 12.00

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming6 Global variable vs. local variable #include void menu(); float determine_price(int); float calc(float,int); void print_result(float); int code,qty;float price,pay; int main() { menu(); printf("Enter item code and quantity:"); scanf("%d %d", &code,&qty); price= determine_price(code); pay=calc(price,qty); print_result(pay); return 0; } void menu( ) { printf("Code\tItem\tPrice\n"); printf("1\tPapaya\t1.00\n"); printf("2\tMelon\t2.00\n"); printf("3\tDurian\t3.00\n"); printf("\tOthers\t4.00\n"); } float determine_price(int code) { code--; switch(code) { case 1:price=1.00;break; case 2:price=2.00;break; case 3:price=3.00;break; default:price=4.00; } return(price); } float calc(float price,int quantity) { pay=pay+1; pay=price*quantity; return(pay); } void print_result(float pay) {printf("Payment is %.2f\n", pay); } yasmin]$ gcc testing2.c yasmin]$./a.out Code Item Price 1 Papaya Melon Durian 3.00 Others 4.00 Enter item code and quantity:1 4 Payment is yasmin]$./a.out Code Item Price 1 Papaya Melon Durian 3.00 Others 4.00 Enter item code and quantity:3 1 Payment is 2.00 modification However, sometimes we need to do some modification from inside a function, using global variable will make things worse!!!

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming7 Pass by Value If a parameter is passed by value, then the value of the original data is copied into the function’s parameter (scope: local variable(s)) In other words, it (i.e. local variable) has its own copy of the data changes to copy do not change original data During program execution, it (i.e. local variable) will manipulate the data stored in its own memory space

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming8 Pass by Value (Example) #include void fun1(int, int); //function prototype int main(void) { int a=5, b=10; printf("Before fun 1\n“); printf(" a = %d b = %d\n”, a, b); fun1(a, b); //function call printf("\nAfter fun 1\n“); printf(" a = %d b = %d\n”, a, b); return 0; } void fun1(int aa, int bb) //function definition { aa++; bb--; printf("\n\nInside fun 1\n)"; printf("aa = %d bb = %d\n”, aa, bb); } Output Before fun 1 a = 5 b = 10 Inside fun 1 aa = 6 bb = 9 After fun 1 a = 5 b = 10

UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming9 End Functions (2) Q & A!