Lecture 04 FUNCTIONS AND ARRAYS

Similar documents
Lecture 04 FUNCTIONS AND ARRAYS

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

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

C Functions. 5.2 Program Modules in C

Functions. Systems Programming Concepts

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

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

C++ Programming Lecture 11 Functions Part I

C Programming for Engineers Functions

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

CS101 Introduction to computing Function, Scope Rules and Storage class

CSE123. Program Design and Modular Programming Functions 1-1

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

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Computer Science & Engineering 150A Problem Solving Using Computers

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

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

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

Introduction to Programming

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

Programming for Engineers Functions

Function Call Stack and Activation Records

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

Chapter 5 C Functions

Chapter 3 - Functions

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Fundamentals of Programming Session 13

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

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

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

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

C Functions Pearson Education, Inc. All rights reserved.

Object Oriented Methods : Deeper Look Lecture Three

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

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

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

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

Lab Instructor : Jean Lai

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

Unit 7. Functions. Need of User Defined Functions

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

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

Functions and Recursion

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

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

Methods (Deitel chapter 6)

Methods and Data (Savitch, Chapter 5)

Introduction. What is function? Multiple functions form a larger program Modular programming

C Programming Language

Fundamentals of Programming Session 12

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

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

6.096 Introduction to C++

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

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

Lecture 9 - C Functions

Methods (Deitel chapter 6)

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

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

Chapter 4. Procedural Abstraction and Functions That Return a Value. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CSE 230 Intermediate Programming in C and C++ Functions

Outline. Functions. Functions. Predefined Functions. Example. Example. Predefined functions User-defined functions Actual parameters Formal parameters

Introduction to C Final Review Chapters 1-6 & 13

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Lecture 5: Methods CS2301

CS110D: PROGRAMMING LANGUAGE I

Chapter 3 - Functions

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

The C standard library

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Chapter 3 - Functions

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

C: How to Program. Week /Apr/16

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


Static Methods. Why use methods?

CST112--Functions Page 1

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

CS 106 Introduction to Computer Science I

Methods: A Deeper Look

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

JAVA GUI PROGRAMMING REVISION TOUR III

Functions BCA-105. Few Facts About Functions:

Functions and Recursion

CSCI 171 Chapter Outlines

6.5 Function Prototypes and Argument Coercion

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

User Defined Functions

CS110: PROGRAMMING LANGUAGE I

M.CS201 Programming language

(2-2) Functions I H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (January 18, 2019) Washington State University

Iterative Languages. Scoping

CS113: Lecture 4. Topics: Functions. Function Activation Records

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

4. C++ functions. 1. Library Function 2. User-defined Function

C Programming Lecture V

Introduction to Functions in C. Dr. Ahmed Telba King Saud University College of Engineering Electrical Engineering Department

Transcription:

Lecture 04 FUNCTIONS AND ARRAYS 1

Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions to reduce code duplication and increase program modularity. Easy to: Design Implement Test Maintain Reuse 2

Function of Functions Decomposition and abstraction through functions Break up into modules Suppress detail Create new primitive 3

Functions, Informally A function is like a subprogram, a small program inside of a program. The basic idea we write a sequence of statements and then give that sequence a name. We can then execute this sequence at any time by referring to the name. 4

Functions, Informally The part of the program that creates a function is called a function definition. When the function is used in a program, we say the definition is called or invoked. 5

Functions Modules in C Program Modules in C Programs written by combining user-defined functions with library functions C standard library has a wide variety of functions Makes programmer's job easier - avoid reinventing the wheel

Program Modules in C Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Boss asks worker to complete task Worker gets information, does task, returns result Information hiding: boss does not know details 7

Parameters and arguments Inside the function, the values that are passed get assigned to variable called parameters void sing(person) The values that control how the function does its job called arguments (real values) Sing( Bob ) 8

Math Library Functions Math library functions perform common mathematical calculations #include <math.h> Format for calling functions FunctionName (argument); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); 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 9

Functions Functions Modularize a program All variables declared inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables 10

Benefits Divide and conquer Functions Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoids code repetition 11

Function Definitions Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifier Return-value-type: data type of the result (default int) void - function returns nothing Parameter-list: comma separated list, declares parameters (default int) 12

Function Definitions (II) return-value-type function-name( parameter-list ) { declarations and statements } Declarations and statements: function body (block) Variables can be declared inside blocks (can be nested) Function can not be defined inside another function Returning control If nothing returned return; or, until reaches right brace If something returned return expression; 13

Examples 14

Function prototype Function name Function Prototypes Parameters - what the function takes in Return type - data type function returns (default int) Used to validate functions Prototype only needed if function definition comes after use in program int maximum( int, int, int ); Takes in 3 ints Returns an int Promotion rules and conversions Converting to lower types can lead to errors 15

Header files Header Files contain function prototypes for library functions <stdlib.h>, <math.h>, etc Load with #include <filename> #include <math.h> Custom header files Create file with functions Save as filename.h Load in other files with #include "filename.h" Reuse functions 16

Calling Functions: Call by Value Used when invoking functions 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 17

Call by reference (*) Call by Reference Passes original argument Changes in function effect original Only used with trusted functions 18

Storage Classes Storage class specifies Storage duration - how long an object exists in memory Scope - where object can be referenced in program Linkage - what files an identifier is known 19

Automatic storage Storage Classes Object created and destroyed within its block auto: default for local variables auto double x, y; register: tries to put variable into high-speed registers Can only be used for automatic variables register int counter = 1; 20

Static storage Storage Classes Variables exist for entire program execution Default value of zero static: local variables defined in functions. Keep value after function ends Only known in their own function (for different calls of functions) Static functionname makes it invisible outside the file extern: global variables and functions. Known in any function A declaration not a definition 21

Scope Rules File scope Identifier defined outside function, known in all functions Global variables, function definitions, function prototypes Function scope Can only be referenced inside a function body Only labels (start: case:, etc.) 22

Block scope Scope Rules Identifier declared inside a block Block scope begins at declaration, ends at right brace Variables, function parameters (local variables of function) Outer blocks "hidden" from inner blocks if same variable name Function prototype scope Identifiers in parameter list Names in function prototype optional, and can be used anywhere 23

Outline 1. Function prototypes 1.1 Initialize global variable 1.2 Initialize local variable 1.3 Initialize local variable in block 2. Call functions 3. Output results

Outline 3.1 Function definitions