Week 3-4 Control flow (review) Function definition Program Structures

Slides:



Advertisements
Similar presentations
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
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.
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.
 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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© 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 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Engineering Computing I Chapter 4 Functions and Program Structure.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 3 Translation.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Methods Chapter 6.
Functions Separate Compilation
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Functions and Modular Programming
Function.
1-6 Midterm Review.
CS1100 Computational Engineering
Function.
CPS125.
Presentation transcript:

Week 3-4 Control flow (review) Function definition Program Structures Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go; Function definition Return type, parameter definition, parameter passing Function calls and standard library Program Structures Preprocessing Header files, Application Programming Interfaces (API) Scope rules C library

Conditional statements If, else, else if, switch-case, break http://www.cse.ohio-state.edu/~yuw/459-21/switch.c

Control flow Loop constructs Other flow control methods for while do-while Other flow control methods break continue label—go Sample programs calculate the sum of 1-10 using different loops

Function Function prototype Function calls return type argument definition Function calls basic syntax parameter passing Example: find the next prime number

Function Organization A set of functions Defined in the same files as main() Add two more interfaces: my_isuppercase() and my_islowercase() Sample program Use separated file Header files Application Programming Interfaces (API) How to modify the above sample counting.c new_ctype.h new_ctype.c Separated compilation

Recursion A function can call itself Directly Indirectly examples: Calculate the power of x to y Find a matching number in an increasing array of integers

Scope Rules Scope: Local and global variables external variable Where the variable or function definitions can be accessed Local and global variables Local: a variable defined inside a function Global: a variable defined outside of functions external variable Note: global variable and external variables Typically used when a global variable is used in another file

Scope Rules -- continued static variable Value are retained on exit from function Limited scope Only accessible within the same file Or other source files that ‘include’s this file Automatic variables: Typically local variables variables that are automatically created/destroyed Register variable: variables are saved in register for fast access

Function scope All functions are defined externally Static function, limited scope Inline functions

Preprocessing Preprocessing directives file inclusion code replacement #include code replacement #define Others #error, #warning #pragma, machine or platform dependent Conditional compilation #ifdef -- #endif; #if EXP -- #endif Conditional inclusion #ifdef #include #endif

C library Library: Static and shared libraries A collection of object files that contains the function definitions Static and shared libraries Static library Copies of function definitions are compiled into the final executable Shared library Functions are not copied into the final executable Information are recorded in the image so that the correct library will loaded on demand when the program starts up.

C library -- continued Creating libraries on *nix systems Static library, with suffix .a gcc -c file1.o ar cr libc459.a file1.o Shared library, with suffix .so gcc -c -fPIC file1.c gcc -shared -fPIC -o libc459.so file1.o