Programming for Engineers Functions

Similar documents
C Programming for Engineers Functions

C Functions. 5.2 Program Modules in C

Function Call Stack and Activation Records

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Lecture 04 FUNCTIONS AND ARRAYS

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Chapter 5 C Functions

Chapter 3 - Functions

Lecture 04 FUNCTIONS AND ARRAYS

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions and Recursion

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

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

Functions. Systems Programming Concepts

Cpt S 122 Data Structures. Introduction to C++ Part II

C++ How to Program, 9/e by Pearson Education, Inc. All Rights Reserved.

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Introduction to Programming

C++ Programming Lecture 11 Functions Part I

Lecture 9 - C Functions

CSE123. Program Design and Modular Programming Functions 1-1

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

IT 374 C# and Applications/ IT695 C# Data Structures

Beginning C Programming for Engineers

Functions in C C Programming and Software Tools

CME 112- Programming Languages II. Week 1 Introduction, Scope Rules and Generating Random Numbers

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Programming for Engineers Introduction to C

Chapter 4 Functions By C.K. Liang

Chapter 3 - Functions

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Lab Instructor : Jean Lai

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

CSE 230 Intermediate Programming in C and C++ Functions

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

1. Introduction. 2. Project Submission and Deliverables

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

C: How to Program. Week /Apr/23

Programming for Engineers Iteration

Introduction to C Final Review Chapters 1-6 & 13

Computer Science & Engineering 150A Problem Solving Using Computers

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Programming for Engineers Pointers

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

6.5 Function Prototypes and Argument Coercion

Functions. Introduction :

Chapter 7 Functions. Now consider a more advanced example:

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

ECET 264 C Programming Language with Applications

BSM540 Basics of C Language

CS 139 Practice Midterm Questions #2

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Chapter 2, Part I Introduction to C Programming

M.CS201 Programming language

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Functions. Using Bloodshed Dev-C++ Heejin Park. Hanyang University

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

C Programming Language

Lecture 2: C Programming Basic

C Programs: Simple Statements and Expressions

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

ภาคการศ กษาท 1 ป การศ กษา 2556

C Functions Pearson Education, Inc. All rights reserved.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Technical Questions. Q 1) What are the key features in C programming language?

Chapter 2, Part III Arithmetic Operators and Decision Making

A A B U n i v e r s i t y

Programming for Engineers Arrays

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS3157: Advanced Programming. Outline

Chapter 3 - Functions

Programming and Data Structure

Functions BCA-105. Few Facts About Functions:

Programming for Engineers C Preprocessor

C Programming Language

Functions and Recursion

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Programming Assignment #4 Arrays and Pointers

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

Assignment #3 Answers

Methods: A Deeper Look

Fundamentals of Programming Session 25

Two Dimensional Array - An array with a multiple indexs.

Object Oriented Design

Unit 7. Functions. Need of User Defined Functions

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Two Dimensional Array - An array with a multiple indexs.

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

The C standard library

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Transcription:

Programming for Engineers Functions ICEN 200 Spring 2018 Prof. Dola Saha 1

Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller problems / modules Repeated set of statements 2

Example: Area and circumference of a circle 3

Computing Rim Area of a Flat Washer 4

C Code (1) 5

C Code (2) 6

Functions Functions allow us to modularize a program reuse the code Two types: Programmer/user write, called programmer-defined functions prepackaged functions available in the C standard library. Input Variables Output value, which is returned Function body 7

Function The statements defining the function are written only once, and the statements are hidden from other functions. Functions are invoked by a function call, which specifies the function name and provides information (as arguments) that the called function needs to perform its designated task. 8

Modularizing Program Analogy : Hierarchical management A boss (the calling function or caller) asks a worker (the called function) to perform a task and report back when the task is done 9

Function All variables defined in function definitions are local variables they can be accessed only in the function in which they re defined. Most functions have a list of parameters that provide the means for communicating information between functions. A function s parameters are also local variables of that function. The format of a function definition is return-value-type function-name(parameter-list) { definitions statements } 10

Example of User-defined Function 11

Function Definition Function square is invoked or called in main within the printf statement printf("%d ", square(x)); // function call Function square receives a copy of the value of x in the parameter y. Then square calculates y * y. The result is passed back returned to function printf in main where square was invoked, and printf displays the result. This process is repeated 10 times using the for statement. 12

Function Definition cont. The definition of function square shows that square expects an integer parameter y. The keyword int preceding the function name indicates that square returns an integer result. The return statement in square passes the value of the expression y * y (that is, the result of the calculation) back to the calling function. int square(int y); // function prototype The int in parentheses informs the compiler that square expects to receive an integer value from the caller. The int to the left of the function name square informs the compiler that square returns an integer result to the caller. 13

Function Definition cont. The compiler refers to the function prototype to check that any calls to square contain the correct return type the correct number of arguments the correct argument types the arguments are in the correct order The function-name is any valid identifier. The return-value-type is the data type of the result returned to the caller. The return-value-type void indicates that a function does not return a value. Together, the return-value-type, function-name and parameter-list are sometimes referred to as the function header. 14

Function Definition cont. The parameter-list is a comma-separated list that specifies the parameters received by the function when it s called. If a function does not receive any values, parameter-list is void. A type must be listed explicitly for each parameter. The definitions and statements within braces form the function body, which is also referred to as a block. Variables can be declared in any block, and blocks can be nested. 15

Return Control Returns control to calling function after function execution the function does not return a result, control returns immediately after the execution of function body Returns after executing the statement return; Returns the value of the expression to the caller by the statement - return expression; 16

main() s Return Type main has an int return type. The return value of main is used to indicate whether the program executed correctly. In earlier versions of C, we had to explicitly place return 0; at the end of main 0 indicates that a program ran successfully. main implicitly returns 0 if we omit the return statement. We can explicitly return non-zero values from main to indicate that a problem occurred during your program s execution. 17

Function Example: maximum() 18

Function Example: maximum() 19

Write a function to calculate area of a washer s 20

Compiling your own code 1 2 21

Compiling your own code pwd print work directory cd directory_name change directory ls list the content of current directory 3 22

Linking with Math Library gcc o object_filename c_file.c lm -l link to the library -lm is specific for math Run the object file./object_filename 4 23

Math Library Functions Performs common mathematical calculations. 24

More Math Library Functions #include <math.h> 25

Random Number Generation Why? For example, a program that simulates coin tossing might require only 0 for heads and 1 for tails. A dice-rolling program that simulates a six-sided die would require random integers from 1 to 6. The rand function generates an integer between 0 and RAND_MAX (a symbolic constant defined in the <stdlib.h> header). i = rand(); To get a range of values, use remainder operation. i = rand()%n; // random values in {0 to N-1} 26

Scaling and Shifting Generate Random Number r_num = rand(); 0 RAND_MAX Scale r_scaled = r_num()%n; 0 N Shift r_shifted = r_scaled+m; M N+M 27

Random Number Generation Code 28

Pseudorandom numbers Function rand() generates pseudorandom numbers. Calling rand() repeatedly produces a sequence of numbers that appears to be random. Randomizing A program conditioned to produce a different sequence of random numbers for each execution Accomplished with the standard library function srand(). Function srand() takes an unsigned integer argument and seeds function rand() to produce a different sequence of random numbers for each execution of the program. 29

Randomizing with a seed 30

Output 31

Randomize without providing a seed To randomize without entering a seed each time, use a statement like srand(time(null)); The function prototype for time is in <time.h>. Function time returns the number of seconds that have passed since midnight on January 1, 1970. This value is converted to an unsigned integer and used as the seed to the random number generator. 32

Randomize with time 33

Passing argument by value & by pointer Pass by Value A copy of argument s value is made and passed to the function Changes to copy do not change the original value Most commonly used Pass by Pointer An address to the argument is passed to the function Changes to the value of the address does change the original value Should be used by trusted functions only 34

Example Pass-by-value & Pass-by-reference Output 35