C Programming for Engineers Functions

Similar documents
Programming for Engineers Functions

Lecture 04 FUNCTIONS AND ARRAYS

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

Introduction to Programming

C++ Programming Lecture 11 Functions Part I

Programming for Engineers Introduction to C

Lecture 9 - C Functions

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

C Functions. 5.2 Program Modules in C

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

Chapter 2, Part I Introduction to C Programming

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

M.CS201 Programming language

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Programming for Engineers Iteration

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 3 - Functions

CSE 230 Intermediate Programming in C and C++ Functions

Programming for Engineers Arrays

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

Chapter 7 Functions. Now consider a more advanced example:

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

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

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

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

Functions. Systems Programming Concepts

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

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

Functions and Recursion

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

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

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Lecture 2: C Programming Basic

Functions. printf( %f cubed is %f,x,cube(x)); return 0; } return parameter type double cube(double var) { return (var*var*var);

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

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

Lecture 5: Methods CS2301

Functions. Lecture 6 COP 3014 Spring February 11, 2018

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Computer Science & Engineering 150A Problem Solving Using Computers

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

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

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Object Oriented Methods : Deeper Look Lecture Three

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

Programming for Engineers Pointers

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

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

Chapter 2, Part III Arithmetic Operators and Decision Making

Functions BCA-105. Few Facts About Functions:

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

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Chapter 5 C Functions

A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Variables and Operators 2/20/01 Lecture #

6.096 Introduction to C++

Methods (Deitel chapter 6)

Fundamentals of Programming Session 25

Methods (Deitel chapter 6)

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

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

CS 106 Introduction to Computer Science I

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

CS1150 Principles of Computer Science Methods

CS313D: ADVANCED PROGRAMMING LANGUAGE

Function Call Stack and Activation Records

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

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

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CS1150 Principles of Computer Science Methods

C Functions. Object created and destroyed within its block auto: default for local variables

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

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

Functions and Recursion

Computer Programming C++ Classes and Objects 6 th Lecture

Fundamental Concepts and Definitions

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

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

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 6 Introduction to Defining Classes

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Unit 7. Functions. Need of User Defined Functions

UNIT V Sub u P b ro r g o r g a r m a s

CS 106 Introduction to Computer Science I

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Fast Introduction to Object Oriented Programming and C++

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Computer Programming

BSM540 Basics of C Language

Programming for Engineers C Preprocessor

Transcription:

C Programming for Engineers Functions ICEN 360 Spring 2017 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

Classwork Assignment Compute rim area of a flat washer using function to calculate the area. 20

Math Library Functions Performs common mathematical calculations. 21

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

Self Review Assignment Modify the program to compute the rim area of a flat washer using two functions To calculate the area To compute the square 23