Introduction to C Programming

Slides:



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

Introduction to C Programming
C Language.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Guide To UNIX Using Linux Third Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Functions.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
C++ for Engineers and Scientists Third Edition
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
CPS120: Introduction to Computer Science Functions.
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.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
© 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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
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.
Programming Fundamentals Enumerations and Functions.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
A First Book of ANSI C Fourth Edition
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
User-Written Functions
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Chapter 9: Value-Returning Functions
Lesson #6 Modular Programming and Functions.
Object Oriented Programming in java
Submitted By : Veenu Saini Lecturer (IT)
CPS125.
Corresponds with Chapter 5
Presentation transcript:

Introduction to C Programming ET2560 Introduction to C Programming Introduction to C Programming Unit 7 Modular Programming Using Functions Unit 1 Presentations

Top-Down Design Unit 7: Modular Programming

Top-Down Design Break Problem down into sub-problems Use available solutions for any sub-problems already solved Create modules that solve recurring sub-problems C library functions provide pre-written solutions Large real-world problems are large and complex Problem must be broken down to manage complexity Software reuse saves time and increases reliability C language provides the function as unit of modularity Black-box concept, defined inputs and outputs

C Library Functions Contain pre-written and pre-tested solutions Basic functions applicable to wide variety of common problems Input and Output Mathematical Operations Character Manipulation String Manipulation Sorting and Searching Time and Date Other Utility Functions

Functions Unit 7: Modular Programming

C Language Functions - Header Function Header Return Type (or void if no return value) Function Name - C identifier Parameter List int f1(void) … double f2 (int p1, double p2) … void f3 (int p3, char p4) …

C Language Functions - Body Function Body Compound Statement (delimited by brace characters) Last statement: return statement - if value is returned double f2 (int p1, double p2) { /* Function body */ return (value); }

Types of Functions No inputs or outputs - Perform an action Use keyword void for return and parameter list No output, one or more inputs - Perform an action with data Use keyword void for return Parameter list specifies inputs One output only - Retrieve information Specify return type, use keyword void for parameter list One output, one or more inputs - Typical, many uses Specify return type, input parameter list Multiple outputs - Special circumstances scanf() is an example The return statement can only represent one output Multiple outputs require output parameters

Use of a Function - Syntax Syntax: Name of function followed by argument list Parenthesized list, arguments separated by commas If function has no inputs, parenthesized list is empty f1() If function has inputs, appear in argument list f1(a1, a2, a3) Must match the parameter list in number and order Argument values are copied to parameter list

Use of a Function - Function Call Use of function is termed a "function call" A function can be a statement by itself f1(a1, a2); A function with a return value can be part of an expression x = y + f1(a1, a2*4) - 63;

Placement of Function Definitions One or more function definitions in a single file Functions must be placed one after another Cannot have a function definition inside another function A function must be declared before it is used Function declarations accomplished with prototypes Function prototypes should be placed at top of file When creating a large multi-file program, use include file Prototype is like function header, with semicolon instead of body

Function Call: Control of Execution When a function call appears in code: The arguments are evaluated and passed to function Execution transfers to the first statement in function The function executes until a return statement (or it finishes) Execution transfers back to the point of the call Note that the calling function may pause mid-statement A function may call one or more additional functions The program stack is used to manage nesting function calls

Variable Scope Unit 7: Modular Programming

Variable Scope Scope determines lifetime of variable How long variable uses memory How long the data in the variable is valid A variable declared at the top of a function body is local scope Lifetime only while function is active Static keyword modifies this behavior A variable declared outside of a function is file scope File scope is also called "global" scope Lifetime is for entire time program is executing A variable declared inside a compound statement is block scope Lifetime is only while execution is inside the block

Function Output Parameters Unit 7: Modular Programming

Pointers Every variable in memory has a memory address A pointer is a variable that holds a memory address Pointers are used in C for multiple reasons This unit introduces pointers to explain output parameters The pointer can be used to change memory indirectly

Pointers Pointers are declared using the "*" character int * ip; The address of a variable is obtained with the "&" character int x, y; ip = &x; To modify a variable using the pointer, the "*" is used *ip = 5; /* Stores 5 in "x" */ ip = &y; /* Change to point to y */ *ip = 12; /* Stores 12 in "y" */

Output Parameters A function with output parameters uses pointers The output parameter is declared as a pointer using "*" The argument must evaluate to a variable's address Internally, the function changes the variable via the pointer int f1(int * p1, int * p2) { *p1 = 0; *p2 = 27; return (55); }