Introduction to FunctionsCS-2301 B-term 20081 1 Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.

Slides:



Advertisements
Similar presentations
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Advertisements

1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Introduction to C Programming
Functions and InterfacesCS-2303, C-Term Functions and Interfaces in C CS-2303, System Programming Concepts (Slides include materials from The C Programming.
Numerical Computation Review and Continuation CS-2301, B-Term Numerical Computation in C Review and Continuation CS-2301, System Programming for.
CS 201 Functions Debzani Deb.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Discussion of Assignment #2 CS-2301, B-Term Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
Overview creating your own functions calling your own functions.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
CS Winter 2011 Introduction to the C programming language.
CS 201 Functions Debzani Deb.
Information Hiding and Encapsulation
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Conditionals, Loops, and Other Statements CS-2301 D-term Conditionals, Loops, and Other Kinds of Statements CS-2301 System Programming C-term 2009.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Introduction to C Programming
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
Conditionals, Loops, and Other Statements CS-2301, B-Term Conditionals, Loops, and Other Kinds of Statements CS-2301, System Programming for Non-Majors.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Computer Science 210 Computer Organization Introduction to C.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Programming With C.
Functions Why we use functions C library functions Creating our own functions.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
1 ICS103 Programming in C Lecture 7: Introduction to 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.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
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.
1 ICS103 Programming in C Lecture 8: Functions I.
C LANGUAGE Characteristics of C · Small size
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.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Computer Science 210 Computer Organization
Chapter 6: Modular Programming
Quiz 11/15/16 – C functions, arrays and strings
Programmazione I a.a. 2017/2018.
User Defined Functions
Functions and Modular Programming
Introduction to C Topics Compilation Using the gcc Compiler
Programming Assignment #1 12-Month Calendar—
Scope Rules and Storage Types
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Function.
Introduction to C Topics Compilation Using the gcc Compiler
Programming Languages and Paradigms
Function.
CPS125.
Introduction to Classes and Objects
Presentation transcript:

Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language, 2 nd ed., by Kernighan and Ritchie and from C: How to Program, 5 th ed., by Deitel and Deitel)

Introduction to FunctionsCS-2301 B-term Definition – Function A fragment of code that accepts zero or more argument values and produces a result value and zero or more side effects. A method of encapsulating a subset of a program or a system To hide details To be invoked from multiple places To share with others

Introduction to FunctionsCS-2301 B-term Functions – a big Topic Examples Function definition Function prototypes & Header files Pre- and post-conditions Scope and storage class Implementation of functions Recursive functions

Introduction to FunctionsCS-2301 B-term Common Functions #include –sin(x) // radians –cos(x) // radians –tan(x) // radians –atan(x) –atan2(y,x) –exp(x) // e x –log(x) // log e x –log10(x) // log 10 x –sqrt(x) // x  0 –pow(x, y) // x y –... #include –printf() –fprintf() –scanf() –sscanf() –... #include –strcpy() –strcat() –strcmp() –strlen() –...

Introduction to FunctionsCS-2301 B-term Common Functions (continued) In Kernighan & Ritchie – // for diagnostics, loop invariants, etc. – // for parsing arguments – // time of day and elapsed time – // implementation dependent numbers – // implementation dependent numbers. – // beyond scope of this course

Introduction to FunctionsCS-2301 B-term Common Functions (continued) See also the man pages of your system for things like // concurrent execution // network communications... // many, many other facilities Fundamental Rule: if there is a chance that someone else had same problem as you, … … there is probably a package of functions to solve it!

Introduction to FunctionsCS-2301 B-term Functions in C resultType functionName( argType 1 arg1, argType 2 arg2, …) { … body … } If no result, resultType should be void Warning if not! If no arguments, use void between ()

Introduction to FunctionsCS-2301 B-term Functions in C resultType functionName( argType 1 arg1, argType 2 arg2, …) { … body … }// functionName If no result, resultType should be void Warning if not! If no arguments, use void between () It is good style to always end a function with a comment showing its name

Introduction to FunctionsCS-2301 B-term Function Definition Every function definition has the form return-type function-name (argument declarations) { declarations and statements } See top of page 70 in Kernighan & Ritchie

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Function Definition Every function definition has the form return-type function-name (argument declarations) { declarations and statements } See top of page 70 in Kernighan & Ritchie For practical purposes, code between {} (inclusive) is a compound statement

Introduction to FunctionsCS-2301 B-term Note Functions in C do not allow other functions to be declared within them Like C++, Java Unlike Algol, Pascal All functions defined at “top level” of C programs (Usually) visible to linker Can be linked by any other program that knows the function prototype

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Examples double sin(double radians) { … }//sin unsigned int strlen (char *s) { … }//strlen

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Note on printf, etc. int printf(char *s,...) { body }//printf In this function header, “…” is not a professor’s place-holder (as often used in these slides) …but an actual sequence of three dots (no spaces between) Meaning:– the number and types of arguments is indeterminate Use to extract the arguments

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Questions?

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Function Prototypes There are many, many situations in which a function must be used separate from where it is defined – before its definition in the same C program In one or more completely separate C programs This is actually the normal case! Therefore, we need some way to declare a function separate from defining its body. Called a Function Prototype

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Function Prototypes (continued) Definition:– a Function Prototype is a language construct in C with the form:– return-type function-name (argument declarations) ;

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Function Prototypes (continued) Definition:– a Function Prototype is a language construct in C with the form:– return-type function-name (argument declarations) ; I.e., exactly like a function definition, except with a “;” instead of a body in curly brackets

Introduction to FunctionsCS-2301 B-term CS-2301 B-term Purposes of Function Prototype So compiler knows how to compile calls to that function, i.e., –number and types of arguments –type of result As part of a “contract” between developer and programmer who uses the function As part of hiding details of how it works and exposing what it does. A function serves as a “black box.”

Introduction to FunctionsCS-2301 B-term Header files In applications with multiple C programs, function prototypes are typically in header files I.e., the ‘.h ’ files that programmers include in their code Grouped by related functions and features To make it easier for developers to understand To make it easier for team development To make a package that can be used by someone else

Introduction to FunctionsCS-2301 B-term #include Search the system’s directories in order for a file of the name foo.h Directories can be added with ‘ -I ’ switch to gcc command –E.g., gcc –I myProject/include foo.c –Precedes system directories in search order #include “foo.h” Search the directory where the source program is found first, before -I and system directories

Introduction to FunctionsCS-2301 B-term Typical C Programming Style A lot of small C programs, rather than a few large ones Header files to tie them together Makefiles to build or rebuild them in an organized way Later in the term

Introduction to FunctionsCS-2301 B-term Definition – Stub A stub is a dummy implementation of a function with an empty body A placeholder while building a program So that it compiles correctly Fill in one-stub at a time Compile and test if possible

Introduction to FunctionsCS-2301 B-term Questions?

Introduction to FunctionsCS-2301 B-term CS-2301 B-term “Contract” between Developer and User of a Function 1.Function Prototype 2.The pre- and post-conditions –I.e., assertions about what is true before the function is called and what is true after it returns. –A logical way of explaining what the function does

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Definitions Pre-condition:–a characterization or logical statement about the values of the arguments, and values of relevant variables outside the function prior to calling the function Post-condition:–a logical statement or characterization about the result of the function in relation to the values of the arguments and pre-conditions, and changes to values of variables outside the function after the function returns

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Example 1 double sin (double angle); –Pre:– angle is expressed in radians –Post:– result is the familiar sine of angle –Note: this function does not use or change any other variables

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Example 2 int printf (string, arg 1, arg 2, …) –Pre:– string terminated with ‘\0’ and containing conversion codes –Pre:– a buffer maintained by the file system contains zero or more unprinted characters from previous calls. –Post:– args are substituted for conversion codes in copy of string; resulting string is added to buffer –Post:– if ‘\n’ is anywhere in buffer, line is “printed” up to ‘\n’ ; printed characters are cleared from buffer –Post:– result is number of characters added to buffer by printf

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Example 3 float total = 0; int count = 0; int GetNewItem(void) { float input; int rc; printf(“Enter next item:- ”); if ((rc = scanf(“%f”, &input)) != EOF && (rc > 0)) { total += input; count++; };// if return rc; }// GetNewItem

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Example 3 float total = 0; int count = 0; int GetItem(void) { float input; int rc;...; if ((rc = scanf(“%f”, &input)) != EOF && (rc > 0)) { total += input; count++; };// if return rc; }// GetItem Pre:– total is sum of all previous inputs, or zero if none Pre:– count is number of previous inputs, or zero if none Post:– if valid input is received total = total prev + input, count = count prev + 1

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Important Pre- and post-conditions are analogous to loop invariants I.e., they describe something about the data before and after a function is called and the relationship that the function preserves Often are used together with loop invariants … to show that loop invariant is preserved from one iteration to the next

Introduction to FunctionsCS-2301 B-term Introduction to FunctionsCS-2301 B-term Questions?