Introduction to FunctionsCIS 1057 Fall 20131 Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based.

Slides:



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

User Defined Functions
Programming Languages and Paradigms The C Programming Language.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Lecture 2 Introduction to C Programming
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.
CS 201 Functions Debzani Deb.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
"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.
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (Slides include materials from The.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
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.
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
© 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.
Programming With C.
Functions Why we use functions C library functions Creating our own functions.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
1 ICS103 Programming in C Lecture 8: Functions I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
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.
Quiz 11/15/16 – C functions, arrays and strings
CSCI 161: Introduction to Programming Function
Programmazione I a.a. 2017/2018.
User Defined Functions
More about Numerical Computation
Functions and Modular Programming
Introduction to C Topics Compilation Using the gcc Compiler
Miscellaneous C++ Topics
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
Classes, Objects and Methods
Programming Languages and Paradigms
Function.
CPS125.
Introduction to Classes and Objects
Presentation transcript:

Introduction to FunctionsCIS 1057 Fall Introduction to Functions CIS 1057 Computer Programming in C Fall 2013 (Acknowledgement: Many slides based on/borrowed from Professor Hugh C. Lauer. Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Introduction to FunctionsCIS 1057 Fall Definition – Function A fragment of code that accepts zero or more argument values, produces a result value, and has 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 FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Functions in C resultType functionName( type 1 param1, type 2 param2, …) { … body … } If no result, resultType should be void Warning if not! If no parameters, use void between ()

Introduction to FunctionsCIS 1057 Fall Functions in C resultType functionName( type 1 param1, type 2 param2, …) { … body … }// functionName If no result, resultType should be void Warning if not! If no parameters, use void between () It is good style to always end a function with a comment showing its name

Introduction to FunctionsCIS 1057 Fall Using Functions Let int f(double x, int a) be (the beginning of) a declaration of a function. Then f(expr 1, expr 2 ) can be used in any expression where a value of type int can be used – e.g., N = f(pi*pow(r,2), b+c) + d;

Introduction to FunctionsCIS 1057 Fall Let int f(double x, int a) be (the beginning of) a declaration of a function. Then f(expr 1, expr 2 ) can be used in any expression where a value of type int can be used – e.g., N = f(pi*pow(r,2), b+c) + d; Using Functions (continued) This is a parameter This is an argument This is also an argument

Introduction to FunctionsCIS 1057 Fall Definitions Parameter:– a declaration of an identifier within the '()' of a function declaration Used within the body of the function as a variable of that function Initialized to the value of the corresponding argument. Argument:– an expression passed when a function is called; becomes the initial value of the corresponding parameter

Introduction to FunctionsCIS 1057 Fall Let int f(double x, int a) be (the beginning of) a declaration of a function. Then f(expr 1, expr 2 ) can be used in any expression where a value of type int can be used – e.g., N = f(pi*pow(r,2), b+c) + d; Using Functions (continued) The first argument expression is evaluated, converted to double, and assigned to parameter x The second argument expression is evaluated, converted to int, and assigned to parameter a

Introduction to FunctionsCIS 1057 Fall Let int f(double x, int a) be (the beginning of) a declaration of a function. Then f(expr 1, expr 2 ) can be used in any expression where a value of type int can be used – e.g., N = f(pi*pow(r,2), b+c) + d; Using Functions (continued) Function f is executed and returns a value of type int Result of f is added to d Sum is assigned to N

Introduction to FunctionsCIS 1057 Fall Questions?

Introduction to FunctionsCIS 1057 Fall Function Definition Every function definition has the form return-type function-name (parameter declarations) { definitions and statements } For practical purposes, code between {} (inclusive) is a compound statement

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Examples double sin(double radians) { … }//sin unsigned int strlen (char *s) { … }//strlen

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Questions?

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Function Prototypes (continued) Definition:– a Function Prototype is a language construct in C with the form:– return-type function-name (parameter declarations) ;

Introduction to FunctionsCIS 1057 Fall Function Prototypes (continued) Definition:– a Function Prototype is a language construct in C with the form:– return-type function-name (parameter declarations) ; I.e., exactly like a function definition, except with a ';' instead of a body in curly brackets

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Header files In applications with multiple C programs, function prototypes are typically provided 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 FunctionsCIS 1057 Fall #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 FunctionsCIS 1057 Fall 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

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Questions?

Introduction to FunctionsCIS 1057 Fall “ 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 FunctionsCIS 1057 Fall Definitions Pre-condition:–a characterization or logical statement about the values of the parameters, 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 parameters and pre-conditions, and changes to values of variables outside the function after the function returns

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Example 2 int printf (string, arg 1, arg 2, …) –Pre:– string terminated with '\0' and containing conversion specifiers –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 FunctionsCIS 1057 Fall Example 3 /* Pre: fahrenheit value Post:Celsius value */ double f2c(double fahrenheit) { double celsius = 5.0/9.0 * (fahrenheit-32); return celsius; }

Introduction to FunctionsCIS 1057 Fall 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 FunctionsCIS 1057 Fall Questions?