M.CS201 Programming language

Similar documents
C Programming for Engineers Functions

CSE 230 Intermediate Programming in C and C++ Functions

M.CS201 Programming language

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

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

M.CS201 Programming language

Functions. Lecture 6 COP 3014 Spring February 11, 2018

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

Functions BCA-105. Few Facts About Functions:

Chapter 6 : Modularity Using Functions (pp )

Object Oriented Design

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

Lecture 2: C Programming Basic

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

Chapter 2, Part I Introduction to C Programming

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

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

M.EC201 Programming language

CS240: Programming in C

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

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

Object oriented programming C++

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

15 FUNCTIONS IN C 15.1 INTRODUCTION

Fundamental Concepts and Definitions

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Object oriented programming C++

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

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

Unit 7. Functions. Need of User Defined Functions

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.

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

Lecture 04 FUNCTIONS AND ARRAYS

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

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

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

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).

Functions and Recursion

Programming for Engineers Functions

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

Lecture 3 Tao Wang 1

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

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.

User Defined Functions

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

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

2 nd Week Lecture Notes

Functions. (transfer of parameters, returned values, recursion, function pointers).

3. You are writing code for a business application by using C#. You write the following statement to declare an array:

Fundamentals of Programming Session 4

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

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Chapter 4. Defining Classes I

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Computer Programming

Computer Programming C++ Classes and Objects 6 th Lecture

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Object oriented programming with C++

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Introduction to C Final Review Chapters 1-6 & 13

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

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

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

C FUNCTIONS -INDEPENDENT ROUTINE WHICH DO A SPECIFIC TASK(S)- 1/66

Pace University. Fundamental Concepts of CS121 1

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

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

Lecture 8 Tao Wang 1

CS110: PROGRAMMING LANGUAGE I

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 2 Tao Wang 1

Binomial pricer (1.1)

CSc Introduction to Computing

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

Arithmetic Expressions in C

Smaller, simpler, subcomponent of program Provides abstraction

Review of the C Programming Language for Principles of Operating Systems

Learning to Program with Haiku

Here's how you declare a function that returns a pointer to a character:

Prof. Carl Schultheiss MS, PE. CLASS NOTES Lecture 12

BSM540 Basics of C Language

Review. Modules. CS 151 Review #6. Sample Program 6.1a:

UNIT 3 FUNCTIONS AND ARRAYS

More loops Ch

Lecture 3: C Programm

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Java Bytecode (binary file)

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Week 5 Lecture 3. Compiler Errors

C Programming Lecture V

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

Programming for Engineers Introduction to C

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Some Computer Preliminaries

Transcription:

Power Engineering School M.CS201 Programming language Lecture 4 Lecturer: Prof. Dr. T.Uranchimeg

Agenda How a Function Works Function Prototype Structured Programming Local Variables Return value 2

Function Prototype Examples double squared( double number ); void print_report( int report_number ); int get_menu_choice( void ); 3

Function Definition Examples 4

Structured Programming It's easier to write a structured program, because complex programming problems are broken into a number of smaller, simpler tasks. Each task is performed by a function in which code and variables are isolated from the rest of the program. 5

Debug It's easier to debug a structured program. If your program has a bug a structured design makes it easy to isolate the problem to a specific section of code. 6

Writing a Function The first line of every function is the function header, which has three components, each serving a specific function. 7

The Function Return Type The function return type specifies the data type that the function returns to the calling program. The return type can be any of C's data types: char, int, long, float, or double. You can also define a function that doesn't return a value by using a return type of void. 8

Example 9

The Function Name You can name a function anything you like, as long as you follow the rules for C variable names. A function name must be unique. It's a good idea to assign a name that reflects what the function does. 10

The Parameter List Many functions use arguments, which are values passed to the function when it's called. A function needs to know what kinds of arguments to expect--the data type of each argument. Argument type information is provided in the function header by the parameter list. 11

Continue For each argument that is passed to the function, the parameter list must contain one entry. This entry specifies the data type and the name of the parameter. 12

A parameter A parameter is an entry in a function header; it serves as a "placeholder" for an argument. A function's parameters are fixed; they do not change during program execution. 13

An argument An argument is an actual value passed to the function by the calling program. Each time a function is called, it can be passed different arguments. In C, a function must be passed the same number and type of arguments each time it's called, but the argument values can be different. In the function, the argument is accessed by using the corresponding parameter name. 14

Difference between arguments and parameters 15

The Function Body The function body is enclosed in braces, and it immediately follows the function header. It's here that the real work is done. When a function is called, execution begins at the start of the function body and terminates when a return statement is encountered or when execution reaches the closing brace. 16

Local Variables You can declare variables within the body of a function. Variables declared in a function are called local variables. The term local means that the variables are private to that particular function and are distinct from other variables of the same name declared elsewhere in the program. 17

Local variables Local variables can also be initialized when they are declared. You can declare any of C's variable types in a function. Here is an example of four local variables being declared within a function: 18

Example 19

Explain The preceding declarations create the local variables a, b, rate, and cost, which can be used by the code in the function. Note that the function parameters are considered to be variable declarations, so the variables, if any, in the function's parameter list also are available. 20

Continue When you declare and use a variable in a function, it is totally separate and distinct from any other variables that are declared elsewhere in the program. This is true even if the variables have the same name. 21

A demonstration of local variables 22

Continue 23

Result local variables x and y in the function are totally independent from the global variables x and y declared outside the function. 24

Function Statements There is essentially no limitation on the statements that can be included within a function. The only thing you can't do inside a function is define another function. 25

Returning a Value To return a value from a function, you use the return keyword, followed by a C expression. When execution reaches a return statement, the expression is evaluated, and execution passes the value back to the calling program. The return value of the function is the value of the expression. 26

Return value 27

Return value When this function is called, the statements in the function body execute up to the return statement. The return terminates the function and returns the value of x to the calling program. The expression that follows the return keyword can be any valid C expression. 28

Return value A function can contain multiple return statements. The first return executed is the only one that has any effect. Multiple return statements can be an efficient way to return different values from a function 29

Passing Arguments to a Function To pass arguments to a function, you list them in parentheses following the function name. The number of arguments and the type of each argument must match the parameters in the function header and prototype. 30

Continue If a function is defined to take two type int arguments, you must pass it exactly two int arguments--no more, no less--and no other type. If you try to pass a function an incorrect number and/or type of argument, the compiler will detect it, based on the information in the function prototype. 31

Passing Arguments Each argument can be any valid C expression: a constant, a variable, a mathematical or logical expression, or even another function. 32

Example If half(), square(), and third() are all functions with return values, you could write x = half(third(square(half(y)))); The program first calls half(), passing it y as an 33

Example The following is an equivalent piece of code: a = half(y); b = square(a); c = third(b); x = half(c); 34

Calling Functions There are two ways to call a function. Any function can be called by simply using its name and argument list alone in a statement, as in the following example. If the function has a return value, it is discarded. wait(12); 35

Continue The second method can be used only with functions that have a return value. Because these functions evaluate to a value they are valid C expressions and can be used anywhere a C expression can be used. 36

Example In the following example, half_of() is a parameter of a function: printf("half of %d is %d.", x, half_of(x)); First, the function half_of() is called with the value of x, and then printf() is called using the values x and half_of(x). 37

Recursion The term recursion refers to a situation in which a function calls itself either directly or indirectly. Indirect recursion occurs when one function calls another function that then calls the first function. 38

Example 39

Continue 40

Continue 41

Summary How a Function Works Function Prototype Structured Programming Local Variables Return value 42

Any questions? 43

Thank you for attention 44