KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Lecture 10: Don't Reinvent the Wheel. Exam Results.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
Introduction to Computers and Programming Introduction to Methods in Java.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
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.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Functions Review.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Dr. Shady Yehia Elmashad
Functions and an Introduction to Recursion
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 Methods: A Deeper Look
Chapter 6 - Functions Outline 5.1 Introduction
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
6 Functions.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

KIC/Computer Programming & Problem Solving 1

 Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function Prototypes Outline KIC/Computer Programming & Problem Solving 2

Objectives In this chapter, you will learn:  To understand how to construct programs modularly from small pieces called functions..  To introduce the common math functions available in the C standard library.  To be able to create new functions.  To understand the mechanisms used to pass information between functions. KIC/Computer Programming & Problem Solving 3

Introduction Divide and Conquer  Construct a program from smaller pieces or components These smaller pieces are called modules  Each piece more manageable than the original program KIC/Computer Programming & Problem Solving 4 Main program Function 3 Function 1 Function 2 Function 5 Function 4

Program Modules in Ca Functions – Modules in C – Programs combine user-defined functions with library functions C standard library has a wide variety of functions Function calls – Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results – Function call analogy: Boss asks worker to complete task – Worker gets information, does task, returns result – Information hiding: boss does not know details KIC/Computer Programming & Problem Solving 5

Program Modules in C Fig. 5.1 Hierarchical boss function/worker function relationship. KIC/Computer Programming & Problem Solving 6

Math Library Functions Math library functions – perform common mathematical calculations – #include Format for calling functions – FunctionName( argument ); If multiple arguments, use comma-separated list – printf( "%.2f", sqrt( ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double – Arguments may be constants, variables, or expressions KIC/Computer Programming & Problem Solving 7

8

Functions – Modularize a program – All variables defined inside functions are local variables Known only in function defined – Parameters Communicate information between functions Local variables Benefits of functions – Divide and conquer Manageable program development – Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) – Avoid code repetition KIC/Computer Programming & Problem Solving 9

Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements }  Function-name: any valid identifier  Return-value-type: data type of the result (default int ) void – indicates that the function returns nothing  Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter unless, the parameter is of type int KIC/Computer Programming & Problem Solving 10

Function Definitions  Definitions and statements: function body (block) Variables can be defined inside blocks (can be nested) Functions can not be defined inside other functions  Returning control If nothing returned – return; – or, until reaches right brace If something returned – return expression ; KIC/Computer Programming & Problem Solving 11 Data type Function_name (Passing_Data);

Return_Data_Type Function_Name (Passing_Data) { Statment 1; Statment 2; Statment 3; return Val; } void print() { printf(“Build_Functions”); } Function Definitions

KIC/Computer Programming & Problem Solving 13

KIC/Computer Programming & Problem Solving 14

KIC/Computer Programming & Problem Solving 15

KIC/Computer Programming & Problem Solving 16

Function Prototypes Function prototype  Function name  Parameters – what the function takes in  Return type – data type function returns (default int )  Used to validate functions  Prototype only needed if function definition comes after use in program  The function with the prototype int maximum( int x, int y, int z ); Takes in 3 ints Returns an int  Promotion rules and conversions – Converting to lower types can lead to errors KIC/Computer Programming & Problem Solving 17

Function Prototypes KIC/Computer Programming & Problem Solving 18

KIC/Computer Programming & Problem Solving 19 Example Write a program that prints the following consider output numbers resulting from the mathematical relationship

KIC/Computer Programming & Problem Solving 20 Example output program