BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.

Slides:



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

Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
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.
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.
 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.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
© 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. 5.3Math Library Functions Math library functions –perform.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Programming Fundamentals Enumerations and Functions.
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.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 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.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
Functions.
Functions and an Introduction to Recursion
5 C Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
A First Book of ANSI C Fourth Edition
Chapter 9: Value-Returning Functions
CS149D Elements of Computer Science
1-6 Midterm Review.
6 Functions.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

BIL 104E Introduction to Scientific and Engineering Computing Lecture 4

Introduction Divide and Conquer – Construct a program from smaller pieces or components These smaller pieces are called modules Functions – Modules in C – Programs combine user-defined functions with library functions C standard library has a wide variety of functions Lecture 42

Math Library Functions Math library functions – perform common mathematical calculations – #include Format for calling functions – FunctionName( argument ); If multiple arguments, use comma-separated list – printf( "%.2f", sqrt( ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double – Arguments may be constants, variables, or expressions Lecture 43

Math Library Functions Lecture 44

User-Defined Functions Functions (or modules) are the sets of statements that typically perform an operation or that compute a value. To maintain simplicity and readability in longer and more complex problem solutions, instead of using one long main function, we develop programs that use a main function plus additional functions. The execution of a program always begins with the main function. Additional functions are called, or invoked, when the program encounters function names. These additional functions must be defined in the file containing the main function or in another file or library of files. After executing the statements in a function, the program execution continues with the statement that called the function Lecture 45

Advantages of Modular Programming A module can be written and tested separately from other parts of the solution; thus, module development can be done in parallel for large projects. A module is a small part of the solution; thus, testing it separately is easier. Once a module is tested carefully, it does not need to be retested before it can be used in new problem solutions.(reusability) The use of modules usually reduces the length of a program, making it more readable. The use of modules promotes the concept of abstraction, which allows the programmer to “hide” the details in modules. This allows us to use modules in a functional sense without being concerned about the specific details Lecture 46

Function Definitions Functions can be defined before or after the main function. However, one function must be completely defined before another function begins; function definitions cannot be nested within each other. A function consists of a definition statement followed by declarations and statements. If the function does not return a value, the type is void. Thus the general form of a function is: return_type function_name (parameter declarations) { declarations; statements; } The parameter declarations represent the information passed to the function. If there are no input parameters (also called arguments), then the parameter declarations should be void. Additional variables used by a function are defined in the declarations Lecture 47

Function Definitions All functions should include a return statement, which has the following general form: return expression; The expression specifies the value to be returned to the statement that referenced the function. The expression type should match the return type indicated in the function definition to avoid potential errors. A void function does not return a value and thus has this general statement: void function_name (parameter declarations) The return statement in a void function does not contain an expression and has the form: return; Lecture 48

Function Prototype Function prototype statements should be included for all functions referenced in a program. The general form of a function prototype is: return_type function_name (parameter_data_types); A function prototype can be included with preprocessor directives, or because a function prototype is defining the type of value being returned by the function, it can also be included with other variable declarations. If a programmer-defined function references other programmer-defined functions, it will also need additional prototype statements Lecture 49

Function Prototype Header files, such as stdio.h and math.h, contain the prototype statements for many of the functions in the Standard C library. If a program references a large number of programmer-defined functions, it becomes cumbersome to include all the function prototype statements. In these cases, a custom header file can be defined that contains the function prototypes and related symbolic constants. A header file must have a filename that ends with a suffix of.h. The file is then referenced with an include statement using double quotes around the file name. Custom header files are often used to accompany routines that are shared by programmers Lecture 410

Parameter List The definition statement of a function defines parameters that are required by the function; these are called formal parameters. Any statement that references the function must include values that correspond to the parameters; these are called actual parameters. When the function is referenced the value in the ‘actual parameters’ are copied to the ‘formal parameters’ and the steps in the function are executed using the new values in the ‘formal parameters’. If a function has more than one parameter, the formal parameters and the actual parameters must match in number, type and order. Valid references to the function can also include expressions and can include other function references Lecture 411

Example Assume that the definition statement of the function f is double f (double x) { … } The following references to function f are valid: printf(“%f \n”, f(x+2.5)); printf(“%f \n”, f(y)); z = x*x + f(2*x) ; w = f(fabs(y)) ; Here, the formal parameter is still x, but the actual parameter changes depending on the reference selected Lecture 412

Lecture

Lecture 414

Lecture 415 Enter three integers: Maximum is: 85 Enter three integers: Maximum is: 85 Enter three integers: Maximum is: 85

Header Files Header files – Contain function prototypes for library functions –,, etc – Load with #include #include Custom header files – Create file with functions – Save as filename.h – Load in other files with # include "filename.h" – Reuse functions Lecture 416

Header Files Lecture 417

Calling Functions: Call by Value and Call by Reference Call by value – Copy of argument passed to function – Changes in function do not effect original – Use when function does not need to modify argument Avoids accidental changes Call by reference – Passes original argument – Changes in function effect original – Only used with trusted functions For now, we focus on call by value Lecture 418

Random Number Generation Function rand function – Load – Returns "random" number between 0 and RAND_MAX (at least 32767) – i = rand(); – Pseudorandom Preset sequence of "random" numbers Same sequence for every function call Scaling – To get a random number between 1 and n – 1 + ( rand() % n ) rand() % n returns a number between 0 and n - 1 Add 1 to make random number between 1 and n – 1 + ( rand() % 6) – number between 1 and Lecture 419

Random Number Generation Function srand function – – Takes an integer seed and jumps to that location in its "random" sequence srand( seed ); – srand( time( NULL ) );/*load */ time( NULL ) – Returns the time at which the program was compiled in seconds – “Randomizes" the seed Lecture 420

Random Number Generation Function example Lecture 421

Random Number Generation Function example Lecture 422

Local vs Global Variables Local variables are defined within a function and include the formal parameters and any other variables declared in function. A local variable can be accessed only in the function that defined it. A local variable has a value when its function is being executed, but its value is not retained when the function is completed. Global variables are defined outside the main function and other programmer defined functions so they can be accessed by any function within the program. To reference a global or an external variable, a declaration within the function must include the keyword extern before the type designation to tell the computer to look outside the function for the variable. It is optional to use extern designation in the original definition of a global variable Lecture 423

Local vs Global Variables The memory assigned to an external variable is retained for the duration of the program. Although an external variable can be referenced from a function, using global variables is generally discouraged. In general, parameters are preferred for transferring information to a function because the parameter is evident in the function prototype, whereas the external variable is not visible in the function prototype. The static storage class is used to specify that the memory for a variable should be retained during the entire program execution. Therefore, if a local variable in a function is given a static storage class assignment by using the keyword static before its type specification, the variable will not lose its value when the program exits the function in which it is defined. Summary – static: local variables defined in functions. Keep value after function ends Only known in their own function – extern: default for global variables and functions Known in any function Lecture 424

Local vs Global Variables Example Lecture 425

Local vs Global Variables Example Lecture 426

Local vs Global Variables Example Lecture 427

Local vs Global Variables Example local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is Lecture 428