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

Similar documents
Chapter 3 Function Basics

CSE 230 Intermediate Programming in C and C++ Functions

CSCI 1061U Programming Workshop 2. Function Basics

Lecture 04 FUNCTIONS AND ARRAYS

Function Call Example

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

Recap: Functions as first-class values

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

UNIT III (PART-II) & UNIT IV(PART-I)

C Programming for Engineers Functions

Programming Languages

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Functions in C C Programming and Software Tools

Topics. Functions. Functions

Lecture 12 Modular Programming with Functions

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 8: Intraprogram Communication. Lecture 8 1

C & Data Structures syllabus

Lecture 04 FUNCTIONS AND ARRAYS

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

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

EL6483: Brief Overview of C Programming Language

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

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

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

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

A Fast Review of C Essentials Part II

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).


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

Static Members. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

JAVA GUI PROGRAMMING REVISION TOUR III

Computer Programming: C++

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

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

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

Chapter 5. Names, Bindings, and Scopes

M.EC201 Programming language

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

User Defined Functions

M.CS201 Programming language

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

G Programming Languages - Fall 2012

Short Notes of CS201

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 7 - Notes User-Defined Functions II

Functions. Lecture 6 COP 3014 Spring February 11, 2018

University of Kelaniya Sri Lanka

CSE 303: Concepts and Tools for Software Development

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

CS-201 Introduction to Programming with Java

CS201 - Introduction to Programming Glossary By

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:

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

C LANGUAGE AND ITS DIFFERENT TYPES OF FUNCTIONS

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Chapter 7 Constructors and Other Tools. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

OBJECTIVE QUESTIONS: Choose the correct alternative:

Variables and Bindings

QUIZ. What is wrong with this code that uses default arguments?

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

CSCI 171 Chapter Outlines

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

CSI33 Data Structures

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Informatica 3 Syntax and Semantics

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

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Functions and Recursion

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Lecture 8 Tao Wang 1

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 7 Functions. Now consider a more advanced example:

Non-instantiable Classes

Computers Programming Course 5. Iulian Năstac

What Is a Function? Illustration of Program Flow

Unit 7. Functions. Need of User Defined Functions

Runtime Environments I. Basilio B. Fraguela

Model Viva Questions for Programming in C lab

One Ring to rule them all, One Ring to bring them all,

C++ Programming Lecture 11 Functions Part I

CSCE 314 Programming Languages. Type System

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

Classes and Objects. Class scope: - private members are only accessible by the class methods.

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Chapter 7. Constructors and Other Tools. Copyright 2016 Pearson, Inc. All rights reserved.

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

The Three Attributes of an Identifier

Transcription:

Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1

Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components Place smaller pieces into functions Pieces are more manageable than one big program Makes other functions smaller Pieces can be independently implemented and tested 2

Programmer-Defined Functions Readability Function name should indicate operations performed Reuse Functions may be used multiple times in same program Functions may be used in other programs 3

Components of Function Use Three steps to implementing functions 1. Function declaration/prototype If not defined before use 2. Function definition 3. Function call Either prototype or definition must come first Prototype and/or definitions can go in either Same file as main() Separate file so other programs can also use it #include 4

5 Program Function Definition Structure main first (preferred) Top down design Some prototypes required Complete prototyping allows function definition in any order main is last lowest level functions first Bottom up design Prototypes not required main in the middle Confusing: Do not do! main Level 2 Level 2 Level 3 Level 3 Level 3 Level 3 Level 2 Level 2 main Level 2 Level 2

1. Function Declaration/Prototype An informational declaration for compiler Tells compiler how to interpret calls Syntax: <return_type> FnName(<formal-parameter-list>); Formal parameter syntax: <data_type> Parameter-Name Example: 6

Function Declaration/Prototype Placed before any calls Generally above all functions in global space May be placed in declaration space of calling function Example 7

Alternative Function Declaration Function declaration is 'information' for compiler, so Compiler only needs to know: Return type Function name Parameter list Example Formal parameter names not needed but help readability 8

2. Function Definition Actual implementation/code for what function does Just like implementing function main() General format header & basic block: <return-type> fn-name (parameter-list) basic block Example: header 9

Return Statements Syntax: return return-value-expression Two actions Sets return value Transfers control back to 'calling' function Good programming & course requirement: One return per function Return is last statement 10

3. Function Call Using function name transfers control to function 1. Values are passed through parameters 2. Statements within function are executed 3. Control continues after the call For value-returning functions, either Store the value for later use Use the value returned without storing Throw away return value 11

Parameters (Arguments) Formal parameters/arguments In function declaration In function definition's header 'Placeholders' for data sent in 'Variable name' used to refer to data in definition of function Actual parameters/arguments In function call 12

Parameter vs. Argument Names used interchangeably Technically parameter is 'formal' piece while argument is 'actual' piece Parameter! Argument! 13

Functions Calling Functions We're already doing this! main() IS a function calling printf! Only requirement: Function's declaration or definition must appear first Common for functions to call many other functions Function can call itself Recursion 14

Declaring Void Functions Similar to functions returning a value Return type specified as 'void' Example prototype: Return-type is 'void' 15

Declaring Void Functions Nothing is returned Void functions cannot have return statement with an expression Will return at end of function Non-void functions must have return statement with an expression Example definition: 16

Calling Void Functions From some other function, like main(): Cannot be used where a value is required Cannot be assigned to a variable, since no value returned 17

Function documentation Used to aid in program maintenance Comments at non-main definition header Purpose of function Parameters Return Class standard example: 18

main(): Special Recall: main() IS a function 'Special' It is the first function executed Called by operating system or run-time system Can return value to operating system Value can be tested in command scripts Tradition holds it should return an int Zero indicates normal ending of program 19

Scope of Identifier Names Region of a program where identifier is visible Begins at definition within block Ends at end of block Local variables Name given to variables defined within function block Can have different local variables with same name declared in different functions Cannot have duplicate local names within a function 20

Scope Rules Local variables preferred Maintain individual control over data Need to know basis (Hidden) Functions should declare whatever local data needed to 'do their job' 21

Global Scope Names declared 'outside' function bodies Global to all functions in that file Global declarations typical for constants: Declare globally so all functions have scope, can use 22

Global Constants and Global Variables Global variables? Possible, but SELDOM-USED Better alternative is to use parameters Dangerous: no control over usage! We do not use global variables in this class! 23

Block Scope Declare data inside nested blocks Has 'block-scope' Note: All function definitions are blocks! 24

Lifetime How long does it last Allocation Deallocation Normally variables are allocated when defined Normally variables are deallocated at the end of block 25

Static & Lifetime Variable definition modifier keyword: static Static variables are only allocated once Static variables are not deallocated until program ends 26

Programming in C T H E E N D 27