Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Spring Semester 2013 Lecture 5
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Introduction to Methods
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Learners Support Publications Classes and Objects.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
CSCI 161: Introduction to Programming Function
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 4 void Functions
6 Chapter Functions.
Classes and Objects.
A First Book of ANSI C Fourth Edition
Chapter 9: Value-Returning Functions
CS149D Elements of Computer Science
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Submitted By : Veenu Saini Lecturer (IT)
CPS125.
Presentation transcript:

Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module ( or function at it is called in C) is a small self-contained section of an algorithm. In modular design, each major program step is implemented as an independent program module or subprogram. These modules are then combined to form a complete program. It is much easier to develop and test separate smaller modules than it is to test the entire program at once. In fact, most professional programmers agree that modular design is the best strategy to use to write a complex program.

6.2 Functions The C programming language fully supports modular design through the concept of functions. A C function is a self- contained and independent block of statements. Every C program consists of one or more functions. All C functions have equal importance, which means that any C function can call any of the other functions in a program including itself and be called by any other function. The function main() is somewhat special in that program execution begins with the first statement in main(). All C programs must have a function called main().

6.3 The Structure of a C Function A C function consists of two parts: a function header and a body. A C function begins with a header statement. The function header specifies the name of the function, the type of value that the function returns, a list of parameters, and the data type of each parameter. The general syntax of the function header is Type function_name(type1 parm1,type2 parm2,…,typeN parmN) function_name is the name given to the function, type is the data type of the value returned by the function, type1 is the data type of the first parameter, parm1 is the data type of the parameter.

main() { … volume(x);Function main()... } double volume(double radius) { …Function volume()... }

We will show the structure of function as an example. int max (int a, int b, int c)function header { int max_value; max_value = a; if ( b > max_value) max_value = b;function body if ( c > max_value) max_value = c; return (max_value); }

6.4 How to Call Function A function can be invoked in two different ways. If the function does not return a value then function is invoked by simply using its name followed by a list of arguments. Function_name(arg1, arg2,…,argN) The arguments are separated by commas and are enclosed in parentheses. The arguments in the function call correspond to parameters in the function definition.

We will show how to call function from the main program as an example. main() { int x, y, z, largest; … largest = max3 ( x, y, z); … } int max(int a, int b, int c) { int max;... } Main function Function

#include void max3 (int,int,int);/* function prototype */ main() { int x,y,z; x = 10; y = 30; z = 15; max3(x,y,z);/* call function */ } void max3 (int a, int b, int c) { int max; max = a; if (max < b) max = b; if (max < c) max = c; printf(“\n The largest of %d %d %d is %d”, a,b,c, max); }

6.5 Function That Return A Value Almost all functions return a value. The only exception to this is a function of type void. The type of value returned by a function by a function is specified in the function header. For example, the function square defined here returns a value of type float For example float square(float x) { … }

The return statement provides the mechanism for returning a value to the calling function. The syntax of the return statement is return ( expression ); Where expression is any valid C expression. 6.6 Function Prototypes So that the C compiler can correctly interpret function calls it is necessary to declare each C function before it is used in a program. The C language provides a statement for declaring function called a function prototype. A function prototype informs the compiler about the type of value returned by the function and the type and number of arguments that the function expects. For example, the function prototype for the function max3() is int max3 (int, int, int);

6.7 Call by Value A C function is typically passed information from the calling function. The called function uses this information to perform its task and then returns a result to the calling function. Although a function can use the values of the arguments provided by the calling function, it cannot change the value of these arguments. This is because when a function is called the compiler evaluates each argument and makes a copy of the argument. It is this copy that is passed to the function instead of the actual argument itself. This mechanism of passing information to a function is known as call by value. Since the function operates on copies of the arguments, the original arguments in the calling function are not affected by anything that happens within the function.

Example: Call by Value #include void change_it(int); main() { int i = 10; printf(“\n value of I before function is called %d”, i); change_it(i); printf(“\n Value of I after function is called %d”, i); } void change_it(int j) { printf(“\n Initial value of J in function %d”, j); j = 2 * j + 5; printf(“\n Final value of J in function %d”, j); }

6.8 Local Variable Variable that are declared within the body of a function such as temporary variables for storing intermediate results of calculations are local variables. Local variables are visible only to the function in which they are defined and are invisible to all other functions. Thus local variables can be accessed only from within the function. They cannot be accessed by any other function. Local variables are also called automatic variables since they are automatically created each time the function is called and are destroyed after the function has completed execution.

#include void function1 (void); main() { int I = 1; … function1();... } void function1(void) { int I = 10;... }

6.9 Global Variables External or global variables, as they are commonly called, are variables that are defined outside a function. Global variables are visible to all functions that follow the definition. The example that follows creates two global variables seed and new_seed. The variable seed is visible to the function main(), random(),and reseed(). The variable new seed is visible to random() and reseed() but not visible to main(), since it was defined after main().

int random(void) int reseed(void) int seed;/* global variable */ main() {... } int new_seed;/* global variable */ int random(void) { … } int reseed( void) { … }

Any Questions