Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
Introduction to C Programming
1 A Simple C Program /* Take a number multiply it by 10 and display it */ #include main() { int number, result; printf("Type in a number \n"); scanf("%d",
ARDUINO CLUB Session 1: C & An Introduction to Linux.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Introduction to Computers and Programming Introduction to Methods in Java.
CS 201 Functions Debzani Deb.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
Review for midterm exam Dilshad M. Shahid Spring NYU.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
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,
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 1 Introduction to Computers In this chapter we describe the major components of a computer system and explain how information is represented in.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.
CS 108 Computing Fundamentals Notes for Thursday September 10, 2015.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Introduction to Method. Example Java Method ( 1 ) The 2 types (kinds) of methods in Java Class methods Instance methods Methods can do more work than.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 CHAPTER 3 MODULAR PROGRAMMING. 2 Introduction  A library in C is a collection of general purpose and related functions.  2 types of libraries: Standard.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
1 ICS103 Programming in C Lecture 8: Functions I.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Engr 0012 (04-1) LecNotes Engr 0012 (04-1) LecNotes Contrasting MATLAB with C MATLABC language Workspace - interactive computation No real.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
Chapter 7 Text Input/Output Objectives
Lesson #6 Modular Programming and Functions.
Computer Programming Chapter 1: Introduction
Lesson #6 Modular Programming and Functions.
Chapter 7 Text Input/Output Objectives
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Administrative things
Lesson #6 Modular Programming and Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Computer Organization & Compilation Process
Functions I Creating a programming with small logical units of code.
Functions Chapter 3 of the text Motivation:
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 1 of 3 Topics Using Predefined Functions
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
In C Programming Language
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Functions Imran Rashid CTO at ManiWeber Technologies.
Computer Organization & Compilation Process
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

Week 4 – Functions Introduction

Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique In programming, this technique is accomplished using functions Using functions is often referred to as modularity Each function can be thought of as a black box: a function is given some information, does a specific task (eg finding the square root of a number) with that information and returns a result

Function Calls We have already been using some common functions such as printf and scanf We have used functions in our programs without understanding how they actually work – this is often the case when a function is used (remember functions are like a “black box”) A function is called or “invoked” by using the name of the function followed by ( ) and including within the brackets any information that the function needs to do its job.

Program Example using Common Functions The program below uses the functions printf and scanf for formatted input and output. main( ) { int n1; printf(“Enter integer: “); /* the info provided to printf is a text string */ scanf(“%d”,&n1); /* 2 types of info are provided to scanf: the datatype to be read and the address it is to be placed */ printf(“n1: %d\n”, n1);/* the info provided to printf is a text string containing a format string and an integer value */ }

Standard Functions Functions such as printf and scanf format output and obtain input These functions are defined in the stdio.h header file (standard input/output) and we do not need to include the code for them in our program However #include is a preprocessor directive (executed before compiling) to include another file as part of the source code #include will include standard input/output function prototypes, such as for printf and scanf, from the stdio.h header file in the standard library Note: The C compiler on phobos does not require the #include but it is required by many compilers

Program Example using #include Directive The program below includes the preprocessor directive #include because it used the functions printf and scanf which are defined in the stdio.h header file. Note: The C compiler on phobos does not require the #include but it is required by many compilers #include main( ) { int n1; printf(“Enter integer: “); scanf(“%d”,&n1); printf(“n1: %d\n”, n1); }