© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
User Defined Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming The software development method algorithms.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Examples of loops and nested loops.
Introduction to Methods
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
CSCI 171 Presentation 6 Functions and Variable Scope.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
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.
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.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Chapter 3: User-Defined Functions I
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.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Introduction to C++ computers and programming
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Functions CIS 40 – Introduction to Programming in Python
Functions in C Mrs. Chitra M. Gaikwad.
CSCI 161: Introduction to Programming Function
Programmazione I a.a. 2017/2018.
User-Defined Functions
Lesson #6 Modular Programming and Functions.
User Defined Functions
Chapter 4 void Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
In C Programming Language
CPS125.
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions

© Janice Regan, CMPT 102, Sept Modularization  When solving a real scientific (or other) programming problem it will be necessary to break the problem into pieces or modules.  Each module can be separately developed and debugged  Modules can then be assembled and tested to build the solution to the entire problem  When programming in C it is often useful to write each module as a function.  A function is an independent program that solves a problem. That problem is often a component of a larger problem

© Janice Regan, CMPT 102, Sept Modularization  Any module or function can use (call) any other function  When designing a solution for a larger problem it is often useful to draw a map of which functions call which other functions (structure chart or module chart)  A function can even call itself. Such a function is called a recursive function and requires special care to design  Beware of unintended recursion (most commonly function1 calling function2 which calls function1, this type of recursion is also called indirect recursion). This can cause serious problems.

© Janice Regan, CMPT 102, Sept Module Chart main fun5 fun2 fun9 fun8 fun4 fun7 fun6 fun3fun1 recursion Indirect recursion

© Janice Regan, CMPT 102, Sept Types of functions  Functions can be user defined  Written by the user for applications specific to the developers needs  Part of libraries associated with particular tools being used by the developer to built more complex applications Graphics libraries Statistical analysis libraries

© Janice Regan, CMPT 102, Sept Types of functions  Functions can be built in functions  Functions provided by the C language (or other programming language) to accomplish common tasks Trigonometric and mathematical functions Input and output functions

© Janice Regan, CMPT 102, Sept Defining a function  The first line of a function is the function definition  The function has a type  double sinc( double x )  If the type of a function is void it returns no value  Otherwise the function returns a value of the same type as the function  The value of the expression in 1. will be the value returned from the function  The function has 0 or more parameters (arguments)  double sinc( double x ) /*function with 1 argument*/  int printmsg( void ); /*function with no arguments*/

© Janice Regan, CMPT 102, Sept Defining a function  Each of a function’s parameters have types  A function may have parameters of more than one type  In the function definition each parameter must be given its own type  double sinc( double x )  The function has a unique name or identifier  double sinc( double x )  Beware: if you write a function with the same name as a built in function it will replace that built in function  There is no ; at the end of a function definition

© Janice Regan, CMPT 102, Sept Sample Function double sinc(double x) { if (fabs(x) < ) { return(1.0); } else { return( sin(x)/x); } Function definition Function body

© Janice Regan, CMPT 102, Sept Declaring a user’s function  If we wish to use a user supplied function in our main program we must declare that function in our main program  If we wish to use another user supplied function within a function we are writing we must declare that function within the function we are writing  A function prototype (what we call a function declaration) usually follows the variable declarations within the main program or function  A function prototype tells us the name and type of the function, and the types and order of the parameters.  A function prototype looks like the definition of a function followed by a ; parameter identifiers may be omitted double sinc(double x); or double sinc(double);

© Janice Regan, CMPT 102, Sept The body of a function  After the function definition the body of the function is enclosed in { } double sinc(double x) { variable declarations function declarations calculations to determine y return(y); }  When we use the function the expression sinc(myDoubleVariable) appearing in the program calling the function will have value y where y is the sinc of the value of myDoubleVariable

© Janice Regan, CMPT 102, Sept Returning a function’s value:1  In general a simple function will take the supplied values of our parameters, calculate a result, then return that result to the calling program  The function has a type. The type of the function is the type of the value returned by that function to the calling program  A function can return the value of a single variable of a specified type  Later we will look at how to use the parameter list to provide the calling program access to more than one value from the function

© Janice Regan, CMPT 102, Sept Returning a function’s value:2  Our sample function determines the value of sin(x)/x when we supply a value for the parameter x  The function sinc(x) will take the value of x, calculate the value of sin(x)/x and return the resulting value to the calling program  To return the value of the function to the calling program the command return(ValueToBeReturned); is used. The type of ValueToBeReturned should match the type of the function returning the value  A function of any type other than void must contain at least one return statement. It may contain more.

© Janice Regan, CMPT 102, Sept Using a function  Once we have declared a function we can then use it in the body of our function or main program  If we are using a built in function from a C library we need not declare it (the declaration is inside the libname.h file)  To use a function we write an expression of the form  FunctionName(myintvariable, myfloatvariable, myintvariable2);  sinc(MyValue);  The value of the functional expression has the same type as the function and will contain the value returned by the function.  The identifier for a variable that is used in place of a parameter must identify a variable with the same type as that parameter.  It is not necessary to use the same identifier that was used in the function declaration when you use the function, any variable of the right type will do

© Janice Regan, CMPT 102, Sept Example of calling a function #include int main( ) { /* calculate the number of combinations of r objects */ /* chosen from m objects */ /* declare local variables */ int n =12, r= 4, c= 0; /* declare functions by giving prototypes */ int Combination(int m, int r); c = Combination( n, r ); printf("The number of combinations is %d\n", c); return 0; }

© Janice Regan, CMPT 102, Sept Analysis of example (1)  Combination is a function that determine the number of ways r objects can be chosen from m distinct objects (order not significant)  The number of combinations is an integer so the type (return value) of the function is an integer  The numbers of objects in the whole group and the numbers of objects chosen to form a combination are also integers so the arguments of the function are both integers

© Janice Regan, CMPT 102, Sept Analysis of example (2)  The value of the expression Combination(n, r) is the number of combinations. This type of expression is referred to as a call to a function.  In this example this is a call to the function whose identifier (name) is Combination  This call to function Combination will cause the function Combination to be executed to determine the number of combinations of r objects chosen from n objects. The resulting number of Combinations will be assigned to be the value of this expression  The assignment statement below is used to put the value (the number of combinations) into variable c c = Combination (n, r);

© Janice Regan, CMPT 102, Sept Summary  This quick summary of how functions work is enough to help you use the built in functions of the C language or user defined functions supplied to you.  Before you write your own functions we will return to this topic for a more complete discussion