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.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Lecture 10: Don't Reinvent the Wheel. Exam Results.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more.
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.
Introduction to Computers and Programming Introduction to Methods in Java.
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.
Overview creating your own functions calling your own functions.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
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.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
© 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.
Functions Why we use functions C library functions Creating our own functions.
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.
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.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
© 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. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Function. Outline Intro. Functions Examples of Functions Prototypes of a Functions Local and Global Variables.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
UMBC CMSC 104 – Section 01, Fall 2016
Functions Course conducted by: Md.Raihan ul Masood
presented BY : DURGESH KKHANDEKAR 1st semester
Functions, Part 2 of 2 Topics Functions That Return a Value
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Functions Chapter 3 of the text Motivation:
Chapter 6 - Functions Outline 5.1 Introduction
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
C Characters and Strings – Review Lab assignments
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
CS149D Elements of Computer Science
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Functions that return a value
Presentation transcript:

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. Function that sorts a list of names.

Boss Analogy One function can delegate certain jobs to specific functions. Boss Function Max Function Sort Function

Important Concept #1 Divide and Conquer: break large programs into a series of smaller functions. –Helps manage complexity. –Makes it easier to build large programs. –Makes is easier to debug programs.

Important Concept #2 Abstraction: most of the time, you need to know what a function does, but not how it actually does it. –Also helps manage complexity. –You can use other people’s code, without understanding how it works.

Using Pre-Packaged Functions Standard C includes lots of pre-packaged libraries: –ctype.h: character manipulation –math.h: mathematical functions –stdio.h: standard input/output –stdlib.h: random numbers, memory handling –string.h: string manipulation –time.h: date/time functions All of these are detailed in the Book.

Function Concepts Function Prototype Function Definition Function Call

Function Prototype Tells you what type of data the function is expecting, and what type of data the function returns. –Represents a communication protocol that enables two functions to “talk.” –If you want to use a pre-built function, you need to learn to read prototypes.

sqrt Function double sqrt (double); This function therefore accepts one double value, and returns one double value. Return data type Function argument data type

Using sqrt() #include main () { double value; value = sqrt(9.0);/*function call*/ printf ("%f", value); }

Creating your own Functions

#include int square(int); main () { printf("%d ", square(5)); } int square(int y) { return y * y; } Function Prototype: must be declared at top of program. Function Definition: contains the actual function code. Returns a value back up to main. Function Call: invokes the function

Understanding the Program Main Function Square function “Take this Integer!” “Here’s the answer. It’s an integer!”

Function Template Use this template for adding functions to your program. return-value-type function-name (parameter-list); return-value-type function-name (parameter-list) { variable declarations; statements of work; return statement (optional outside of this class); } Function Prototype: must be declared at top of program. Function Definition: contains the actual function code.