More on Functions ANSI-C.

Slides:



Advertisements
Similar presentations
UNIT IV.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Structures Spring 2013Programming and Data Structure1.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
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 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Functions Pass by Value Pass by Reference IC 210.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Computer Science 210 Computer Organization Pointers.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Local Variables A local variable is a variable that is declared within a method declaration. Local variables are accessible only from the method in which.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 –
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 6 Methods Chapter 6 - Methods.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Lecture 10: Structures. Outline Introduction Structure Definitions and declarations Initializing Structures Operations on Structures members Structures.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
1 2-d Arrays. 2 Two Dimensional Arrays We have seen that an array variable can store a list of values Many applications require us to store a table of.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Functions Dr. Sajib Datta
Functions in C Mrs. Chitra M. Gaikwad.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
An Introduction to Java – Part II
Variables and Their scope
Functions, Part 2 of 3 Topics Functions That Return a Value
Reference Parameters.
A function with one argument
Functions Pass By Value Pass by Reference
Topics discussed in this section:
Chapter 9: Value-Returning Functions
Arrays.
Names of variables, functions, classes
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

More on Functions ANSI-C

Function Type and Value A function can return a value. Like all values in C, a function return value has a type. The function is said to have the type of its returned value. /* computes net price of a retail item */ double net ( double gross, int disc, int taxPer){ double discounted = gross – gross*disc/100.0; double tax = discounted*taxPer/100.0; return discounted + tax; } Examples of call: double finalPrice = net( 5.15, 10, 8);

Attributes of a variable name Type Value Location in memory Arguments and parameters must match in: Number Order

Function examples And the call: /* computes net price of a retail item */ double net ( double gross, int disc, int taxPer){ double discounted = gross – gross*disc/100.0; double tax = discounted*taxPer/100.0; return discounted + tax; } And the call: double finalPrice = net( 5.15, 10, 8); Argument values are used to initialize the parameters More specifically: For each parameter Corresponding argument is evaluated A copy of resulting value is given as value to the parameter After parameter initialization, no more communication between argument and parameter exists. This is called: pass-by-value

Assume the main declares /* computes net price of a retail item */ double net ( double gross, int disc, int taxPer){ double discounted = gross – gross*disc/100.0; double tax = discounted*taxPer/100.0; taxPer = 0.0; gross = 0.0; return discounted + tax; } Assume the main declares double gross = 100.0; int discount = 20; int taxRate = 8.6; double total = net(gross, discount, taxRate); printf(“ gross>>%f , tax Rate>>%d”, gross, taxRate); It will print: gross>>100.0, tax Rate>>8.6 /* No CHANGE */

Functions which do not return values The keyword void has two different rolls in this function definition. /* write separator line on output*/ void PrintBannerLines (void) { printf(“***************\n”); } indicates that the function does not return an output value. indicates that the function expects no parameters.

More examples void displayNet( double net){ printf(“The net price of the item is $%7.2f”, net); } void displayNet( double gross, int discRate, int taxRate, double net){ printf(“Item princing information\n”); printf(“ gross price>>$%7.2f\n”, gross); printf(“ discount rate>>$%d\n”, discRate); printf(“ tax rate>>$%d\n”, taxRate); printf(“ net price>>$%7.2f\n\n”, net);