CS1100 Computational Engineering

Slides:



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

Introduction to C Programming
Programming Languages and Paradigms
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Guide To UNIX Using Linux Third Edition
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CPS120: Introduction to Computer Science 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.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
A First Book of ANSI C Fourth Edition
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.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Week 3-4 Control flow (review) Function definition Program Structures
Programming Logic and Design Seventh Edition
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Functions and Structured Programming
Functions, locals, parameters, and separate compilation
JavaScript: Functions
Programming Fundamentals Lecture #7 Functions
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Programming Fundamentals Lecture #7 Functions
CSCI 161: Introduction to Programming Function
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Functions.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
Lesson #6 Modular Programming and Functions.
The Function Prototype
Function.
Method of Classes Chapter 7, page 155 Lecture /4/6.
In C Programming Language
Based on slides created by Bjarne Stroustrup & Tony Gaddis
ETE 132 Lecture 6 By Munirul Haque.
Function.
Functions, Part 2 of 42 Topics Functions That Return a Value
Standard Version of Starting Out with C++, 4th Edition
CPS125.
Presentation transcript:

CS1100 Computational Engineering Functions

Functions = Outsourcing Break large computing tasks into small ones Helps you to build on what others have done You and others write functions When you want to build a program, find out how to use the function and use it Using standard functions provided by the library You are hidden from the implementation Example – you don’t have to worry about how pow(m, n) is implemented As engineers from different disciplines you will use and develop different set of functions 2

Modular Programming Subprograms functions in C, C++, procedures and functions in Pascal facilitate modular programming Overall task is divided into modules Each module - a collection of subprograms a subprogram may be invoked at several points A commonly used computation hiding the implementation changes can be incorporated easily

Example of Function Sets String manipulation Mathematical Finite Element Method Used in structural analysis by Mechanical, Civil, Aero, etc. for stress calculations etc. Most function libraries cost a lot Business opportunity – identify functions that are useful to your area of study, create libraries Functions for use in different software Say, functions for web services 4

Power Function #include <stdio.h> int power (int, int); main( ) { int i; for (i = 0; i < 20; i ++ ) printf(“%d %d %d\n”, i, power(3,i), power(-4,i)); } int power (int base, int n) { int i, p = 1; for ( i = 1; i <= n ; i ++) p = p  base; return p; function prototype Computes the nth power of base (1st parameter) Invocation with arguments A block

Calling Power Function with i=3 j1=power(3,i); j2=power(-4,i)); -64 27 int power (int base, int n){ int i, p = 1; for ( i = 1; i <= n ; i ++) p = p  base; return p; } int power (int base, int n){ int i, p = 1; for ( i = 1; i <= n ; i ++) p = p  base; return p; }

Guarantees termination Recursive Function Example int power (int num, int exp) { int p; if (exp = = 1) return num; p = power(num, exp/2); if (exp % 2 = = 0) return p*p; else return p*p*num;} The base case exp = 1 Guarantees termination

Recursive Function Example power(3, 13) return 1594323 return power(3, 6)*power(3,6)*3 return 729*729*3 return power(3, 3)*power(3,3) return 27*27 return power(3, 1)*power(3,1)*3 return 3*3*3 return 3

In practice int may not be enough! Factorial (n) n! = 1 * 2 * 3 * .... * (n-2) * (n-1) * n Iterative version int fact(int n){    int i;    int result;       result = 1;    for (i = 1; i <= n; i++) result = result * i;       return result; } In practice int may not be enough!

Factorial (n) – Recursive Program n! = n * (n-1)! int fact(int n) {     if (n == 0) return(1);     return (n*fact(n - 1)); } Shorter, simpler to understand Uses fewer variables Machine has to do more work running this one!

Function is a part of your program Basics Function is a part of your program It cannot be a part of any other function main( ) is a function: it is the main function Execution starts there or the control flow starts there From there it can flow from one function to another, return after a computation with some values, probably, and then flow on 11

Transfer of control is affected by calling a function Basics Transfer of control is affected by calling a function With a function call, we pass some parameters These parameters are used within the function A value is computed The value is returned to the function which initiated the call The calling function can ignore the value returned It could use it in some other computation A function could call itself, these are called recursive function calls 12

Add Functions to Your Program A program is a set of variables, and assignments to variables Now we add functions to it Set of variables Some functions including main( ) Communicating values to each other Computing and returning values for each other Instead of one long program, we now write a structured program composed of functions 13

C program -- a collection of functions Features C program -- a collection of functions function main ( ) - mandatory - program starts here. C is not a block structured language a function cannot be defined inside another function only variables can be defined in functions / blocks Variables can be defined outside of all functions global variables - accessible to all functions a means of sharing data between functions - caution Recursion is possible a function can call itself - directly or indirectly

Function – General Form return-type function-name (argument declarations) { declaration and statements return expression; } 15

Function Definition in C return-type function-name (argument declarations) {variable/constant declarations and statements} Arguments or parameters: the means of giving input to the function type and name of arguments are declared names are formal - local to the function Return value: for giving the output value return ( expression ); -- optional To invoke a function function-name(exp1,exp2,…,expn) No function declarations here! Matching the number and type of arguments

Used by the compiler to check the usage Defines Function Prototype Used by the compiler to check the usage prevents execution-time errors Defines the number of parameters, type of each parameter, type of the return value of a function Ex: function prototype of power function: int power ( int, int ); no need for naming the parameters Function prototypes are given in the beginning

More on Functions To write a program You could create one file with all the functions You could/are encouraged to identify different modules and write functions for each module in a different file Each module will have a separate associated header file with the variable declaration global to that module You could compile each module separately and a .o file will be created You can then cc the different .o files and get an a.out file This helps you to debug each module separately 18

Running with Less Memory Functions Provided to break up our problem into more basic units Control flow – flows from function to function, saving the current context, changing contexts, then returning….. Helps the program to run with lesser memory, but slightly slower than a monolithic program without functions Typically functions communicate using the arguments and return values 19

In C, function arguments are passed “by value” Call by Value In C, function arguments are passed “by value” values of the arguments given to the called function in temporary variables rather than the originals the modifications to the parameter variables do not affect the variables in the calling function “Call by reference” variables are passed by reference subject to modification by the function achieved by passing the “address of” variables

Call by Value – An Example main( ) { int p = 1, q = 2, r = 3, s; int test(int, int, int); …; s = test (p, q, r); … /* s is assigned 9 */ } /* p,q,r don’t change, only their copies do */ int test( int a, int b, int c){ a ++; b ++; c ++; return (a + b + c); } Function prototype Function call Function definition

Does not return anything NO Call by Reference #include <stdio.h> void quoRem(int, int, int*, int*); /*addresses or pointers*/ main( ){ int x, y, quo, rem; scanf(“%d%d”, &x, &y); quoRem(x, y, &quo, &rem); printf(“%d %d”, quo , rem); } void quoRem(int num, int den, int* quoAdr, int* remAdr){ *quoAdr = num / den; *remAdr = num % den; Passing addresses Does not return anything

It needs to save some values for future use Pending Computations int fact(int n) { if (n == 1) return 1;     return n * fact(n - 1); } In this recursive version the calling version still has pending work after it gets the return value. (fact 4) 4 * (fact 3) 3 * (fact 2) 2 * (fact 1) 1 2*1 =2 3*2 = 6 4*6 = 24 It needs to save some values for future use

{ return fact_aux(n, 1); } Tail Recursion int fact(n) {     return fact_aux(n, 1); } int fact_aux(int n, int result) {    if (n == 1) return result;    return fact_aux(n - 1, n * result) } The recursive call is in the return statement. The function simply returns what it gets from the call it makes. The calling version does not have to save any values! Auxiliary variable