Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Spring Semester 2013 Lecture 5
Procedural programming in Java
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
VBA Modules, Functions, Variables, and Constants
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Topic R3 – Review for the Final Exam. CISC 105 – Review for the Final Exam Exam Date & Time and Exam Format The final exam is 120-minutes, closed- book,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Introduction to Methods
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Controlling Program Flow with Decision Structures.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Chapter 9: Value-Returning Functions
Programming what is C++
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 4 C Program Control Part I
Functions.
User-Defined Functions
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Introduction to C++ Programming
Chapter 6: User-Defined Functions I
Presentation transcript:

Topic 4 – Programmer- Defined Functions

CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin with the first statement in the main function and end when the main function returns. The ability to create functions assists in the use of top-down design.

CISC 105 – Topic 4 Top-Down Design Top-down design refers to the breaking of a large problem into its component sub-problems. This process is repeated until each sub- problem can be solved easily, typically through the use of a function.

CISC 105 – Topic 4 Top-Down Design This design approach can be illustrated through the development of an algorithm to draw a simple stick-figure. We can divide this drawing process into a three-step process: (1) draw a circle, then (2) draw a plus sign, and, finally, (3) draw an upside down V. Each of the final steps is easily implemented.

CISC 105 – Topic 4 Functions A function is a set of C statements that performs a specific task. Functions take data (their arguments) and return data (their return type). A function can take as many arguments as desired, however, they can only return one piece of data (i.e. an int, or a float, etc...)

CISC 105 – Topic 4 Declaring and Defining a Function To create a programmer-defined function, two components are necessary. First, the function needs a function declaration. This is a list of the function’s return type, the function name, an open paren, the function arguments’ types and names, a close paren, and then a semicolon.

CISC 105 – Topic 4 Function Declarations Therefore, if a function named solve_quad takes two float -type arguments and one int -type argument and returns a float -type, the function declaration would look like: float solve_quad(float x, float y, int z);

CISC 105 – Topic 4 Function Declarations Each function needs to be declared, in the same manner as each variable needs to be declared. All of the function declarations appear after the preprocessor directives and before the main function. Notice that function declarations simply list the functions and their argument and return types; they do not specify the inner workings of the function.

CISC 105 – Topic 4 Function Definitions The second component required of a function is the specification of its inner workings. Thus, a function definition is simply a sequence of statements. The function definition begins just like the main definition, with a function header. It begins with the return type, then the function name, an open paren, the argument list with names for each variable, and a close paren.

CISC 105 – Topic 4 Function Definitions Note that the argument list in the function definition assigns a name to each input argument. Thus, the argument list in the definition also serves as variable declarations for those variables. These variables are initially set to the values passed into the function by the function call.

CISC 105 – Topic 4 Function Definitions Following the function header is an open brace, “{“. After the brace is the sequence of C statements which are to be executed. This typically begins with the declaration of local variables, in the same manner as in the main function. Then, the executable statements that compose the function occur.

CISC 105 – Topic 4 Function Definitions The final statement is a return statement, with one parameter. This parameter is of the data type specified in the function declaration and the function header. This is the value that the function call evaluates to. After the return statement is a closing brace, “}”

CISC 105 – Topic 4 Function Definitions Therefore, to write a program that asks the user for a number, squares it, and displays the result, with the squaring implemented in a function named square_it, the C program would look like:

CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); }

CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); } This is the function declaration. It specifies that there is a function named square_it that takes a float type argument and returns a float.

CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); } This is the function call. It calls the square_it function with its float type argument equal to the value of number. Notice that first the function runs then the expression square_it ( number ) evaluates to the square of number.

CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); } This is the function definition. The header specifies that square_it returns a float and takes a float as argument. Inside the function, this is named x. The function calculates the square of x, and then returns that result.

CISC 105 – Topic 4 Variables and Functions Each variable is only valid within the function in which it is declared. Therefore, the square_it function knows nothing about the number and squared variables declared in main. The main function knows nothing about the result variable declared in square_it. The region, or area of the program, in which a variable is valid is called the scope of the variable.

CISC 105 – Topic 4 Variables and Functions A variable is said to be in scope when it is valid. Thus, any variables declared in (at the beginning of) a function are in scope within the function. A variable is said to be out of scope when it is not valid. Thus, any variables declared in a function are out of scope outside of that function, including in other functions.

CISC 105 – Topic 4 Variables and Functions When a function is called, copies are made of each of the arguments. These copies are placed in the variables declared in the function definition header. The values of the original values in the calling function are not altered when passed to a function during a call.

CISC 105 – Topic 4 Example number squared Before the function call, number and squared are in scope. Space is reserved in memory for each of the two variables. The scanf statement sets the number variable to the user’s input. In this case, we’ll assume the user input x Next, the function square_it is called. The function definition includes the declaration of the x variable. As the number variable is passed into the function, it’s value (6) is copied into the x variable. result The first statement in the square_it function is a declaration of the result variable. Thus, space is reserved in memory. The multiplication is now performed, setting result equal to the square of x, which is equal to the number the user inputted. In this case, the result is The function then reaches the return statement. It returns the value stored in result. Thus, in the main function, the statement square_it(number) evaluates to the value of result, in this case, 36. Thus, squared gets set to

CISC 105 – Topic 4 Example #include float square_it(float x); int main() { float number, squared; printf(“Enter the number>”); scanf(“%f”,&number); squared = square_it(number); printf(“The answer is %f.\n”,squared); return(0); } float square_it(float x) { float result; result = x * x; return (result); }

CISC 105 – Topic 4 Functions with no Return Type Functions do not have to return any data. For such a function, the return type in the function declaration and function definition is of data type void. For such a function, there need not be a return statement. The function ends when all of the statements in the definition complete or when a return statement is encountered. If a return statement is placed in such a function, it takes no arguments.

CISC 105 – Topic 4 Functions with no Return Type A common use for such functions is to produce output. Such an output function would display some text or some data. As the only purpose of such a function would be to produce output, there does not need to be a return type.

CISC 105 – Topic 4 Functions with no Arguments Functions also do not need to receive any arguments. For such a function, the argument list in the declaration and definition is composed of the keyword void, or is simply left blank. A common use for such functions is to obtain necessary input from the user. There need not be any data input into the function; it simply asks for appropriate input and returns the user’s value.

CISC 105 – Topic 4 Examples Thus, a function that takes no input arguments, and returns a double data type, would look like: double function1(void); int main() {... } double function1(void) {... }

CISC 105 – Topic 4 Examples Thus, a function that takes 2 double input arguments, and returns no data would look like: void function2(double x, double y); int main() {... } void function2(double x, double y) {... }

CISC 105 – Topic 4 Examples Thus, a function that takes no input arguments, and returns no data would look like: void function3(void); int main() {... } void function3(void) {... }

CISC 105 – Topic 4 So… Why do we use Functions? Using functions in writing programs allows two primary benefits. First, this approach allows for extensive code reuse. Once a function is written, it can be called many times in a program. For a program that frequently performs certain tasks, this makes coding easier.

CISC 105 – Topic 4 Code Reuse For example, if twenty lines of instructions for a program are displayed more than once in that program, the statements which display that text can be put into a function and called whenever they are needed. This removes the need to copy and paste code at multiple points in a program. If the program is in the debug stage, this also eliminates the need to change multiple sections.

CISC 105 – Topic 4 Abstraction Functions also allow abstraction. Once a problem is divided into subproblems, each subproblem can be implemented in its own function. Thus, the main function is simply a set of function calls, solving one subproblem, then the next subproblem, then the next, etc… This makes program comprehension and maintenance much easier.

CISC 105 – Topic 4 Abstraction If the specifications of one subproblem change, only that function needs to be changed. As that subproblem is solved within the function, no other functions need to change. An example would be a tax program. If one function, calculate_tax, calculates the tax owed using a formula, only that function needs to change if the method of calculating tax changes.