1 ICS103 Programming in C Lecture 7: Introduction to Functions.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Methods Java 5.1 A quick overview of methods
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Representation of Data Types Comparing double and int data types Using integers are faster and more precise round-off errors when using doubles The range.
ICS103: Programming in C 3: Top-Down Design with Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
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.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
1 ICS103 Programming in C Ch3: Top-Down Design with Functions.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS /1433 – Term 1.
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
Chapter 6: User-Defined Functions I
Library Functions Goals of software engineering reliable code
Functions, Part 2 of 2 Topics Functions That Return a Value
ICS103 Programming in C Lecture 3: Introduction to C (2)
Representation of data types
CSCI 161: Introduction to Programming Function
User-Defined Functions
Chapter 6: User-Defined Functions I
Introduction to Problem Solving and Programming
CPS125.
Computer Science II CS132/601* Lecture #C-1.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

1 ICS103 Programming in C Lecture 7: Introduction to Functions

2 Outline Introduction to Functions Predefined Functions and Code Reuse User-defined void Functions without Arguments  Function Prototypes  Function Definitions  Placement of Functions in a Program  Program Style

3 Introduction to Functions … A function can be thought of as a black box that takes one or more input arguments and produces a single output value. For example, the following shows how to use the sqrt function that is available in the standard math library: y = sqrt (x); If x is 16, the function computes the square root of 16. The result, 4, is then assigned to the variable y. The expression part of the assignment statement is called function call. Another example is: z = sqrt (w); If w = 9, z is assigned , which is 8.7.

4 Example /* Performs three square root computations */ #include /* definitions of printf, scanf */ #include /* definition of sqrt */ int main(void) { double first, second, /* input - two data values*/ first_sqrt, /* output - square root of first input */ second_sqrt, /* output - square root of second input */ sum_sqrt; /* output - square root of sum*/ printf("Enter a number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("Square root of the number is %.2f\n", first_sqrt); printf("Enter a second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("Square root of the second number is %.2f\n", second_sqrt); sum_sqrt = sqrt(first + second); printf("Square root of the sum of the 2 numbers is %.2f\n",sum_sqrt); return (0); }

5 Predefined Functions and Code Reuse The primary goal of software engineering is to write error-free code. Reusing code that has already been written & tested is one way to achieve this. --- ”Why reinvent the wheel?” C promotes reuse by providing many predefined functions. e.g.  Mathematical computations.  Input/Output: e.g. printf, scanf

6 Some Mathematical Library Functions FunctionHeader File Purpose ArgumentsResult abs(x) fabs(x) Returns the absolute value of its integer argument x. Returns the absolute value of its double argument x. int double int double sin(x),cos(x), tan(x) Returns the sine, cosine, or tangent of angle x. double (in radians) double log(x) Returns the natural log of x. double (must be positive) double Log10(x) Returns base 10 log of x Double (positive)double pow(x, y) Returns x y double, doubledouble sqrt(x) double (must be positive) double

7 Function Prototypes Like other identifiers in C, a function must be declared before it can be used in a program. To do this, you can add a function prototype before main to tell the compiler what functions you are planning to use. A function prototype tells the C compiler: 1.The data type the function will return –For example, the sqrt function returns a type of double. 2.The function name 3.Information about the arguments that the function expects. –The sqrt function expects a double argument. So the function prototype for sqrt would be: double sqrt(double);

8 Function Prototypes : void Functions void instruct(void); is a void function  Void function - does not return a value –The function just does something without communicating anything back to its caller.  If the arguments are void as well, it means the function doesn’t take any arguments. Now, we can understand what our main function means: int main(void) This means that the function main takes no arguments, and returns an int

9 Function Definition The prototype tells the compiler what arguments the function takes and what it returns, but not what it does. We define our own functions just like we do the main function  Function Header – The same as the prototype, except it is not ended by the symbol ;  Function Body – A code block enclosed by {}, containing variable declarations and executable statements. In the function body, we define what actually the function does  In this case, we call printf 5 times to display user instructions.  Because it is a void function, we can omit the return statement. Control returns to main after the instructions are displayed.

10 Placement of Functions in a program In general, we will declare all of our function prototypes at the beginning (after #include or #define ) This is followed by the main function After that, we define all of our functions. However, this is just a convention. As long as a function’s prototype appears before it is used, it doesn’t matter where in the file it is defined. The order we define them in does not have any impact on how they are executed

11 Execution Order of Functions Execution order of functions is determined by the order of execution of the function call statements. Because the prototypes for the function subprograms appear before the main function, the compiler processes the function prototypes before it translates the main function. The information in each prototype enables the compiler to correctly translate a call to that function. After compiling the main function, the compiler translates each function subprogram. At the end of a function, control always returns to the point where it was called.

12 Flow of Control Between the main Function and a Function Subprogram

13 Program Style Each function should begin with a comment that describes its purpose. If the function subprograms were more complex, we would include comments on each major algorithm step just as we do in function main. It is recommended that you put prototypes for all functions at the top, and then define them all after main.