C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Spring Semester 2013 Lecture 5
Modular Programming With Functions
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
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.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions Dilshad M. Shahid New York
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
A First Book of ANSI C Fourth Edition
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
Functions and an Introduction to Recursion
C Functions -Continue…-.
C++.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Chapter 6 - Functions Outline 5.1 Introduction
Chapter 9: Value-Returning Functions
In C Programming Language
Presentation transcript:

C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The calling function asks the called function to perform a task. When the task is done, the called function reports back to the calling function.

Functions Functions allow the programmer to modularize a program. All variables declared in functions are local variables – they are known only in the function in which they are defined. Format: return-value-type function-name( parameter-list) { declarations statements }

NOTE: function-name valid user defined identifier return-value-type data type of the result returnedto the caller ( parameter-list ) list of parameters passed to the function The return-value-type void indicates that a function does not return a value. By default, this return-value-type is int. If a function does not receive any parameters, the parameter list is void.

main() Function main() follows the similar syntax. void main() { /* declarations */ /* statements */ }

Returning control to the calling function Control is returned when the function ending right brace } is reached. The statement return; also passes control to the calling function. If a function returns a result to the calling function, the statement return expression; is written in the body of the called function to return the value of the expression to the caller.

int main() { /* declarations */ /* statements */ return 0; } Here, return-value-type of main() is int.

Function Prototypes A function prototype tells the compiler: type of data returned number of parameters the function will receive data types of parameters order in which these parameters are expected. The compiler uses function prototypes to validate function calls. A function call that not match the function prototype causes a syntax error. The function prototype must match the function definition.

Program to find the maximum of two integers #include int maximum(int, int); int main() { int a,b; printf(“Enter two integers: \n”); scanf(“ %d %d “, &a,&b); printf(“Maximum is %d \n”,maximum(a,b)); return 0; } int maximum(int x, int y) { int max = x; if ( y > max) max = y; return max; }

Call by Value Call by value When arguments are passed call by value, a copy of the argument’s value is made and passed to the called function. Changes to the copy do not affect an original variable’s value in the calling function.

Recursion A recursive function is a function that calls itself through another function. Factorial of an integer n! n! = n (n-1)(n-2)(n-3)…….1 1! = 1 2! = 2 (1) = 2 3! = 3 (2) (1) = 6 3! is also 3 (2!) = 6 because 2! = 2(1) 4! = 4 (3) (2) (1) = 24 4! is also 4 (3!) = 24

Expressing factorial of n as a recursive function 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Therefore, n! = n (n-1)!

Program to find the factorial of an integer #include int factorial( int ); int main() { int j; printf(“Enter an integer \n”); scanf(“%d”,&j); printf(“Factorial is %d \n”,factorial(j)); return 0; } int factorial( int number) { if (number <= 1) return 1; else return (number * factorial (number - 1)); }

Storage Classes A storage class determines a storage duration and scope. Storage duration – Period during which the identifier exists in memory Storage scope – Where the identifier can be referenced in the program Some identifiers can be referenced through out the program. Other identifiers are referenced only in certain portions of the program.

C provides 4 storage class specifiers : auto, register, extern, static The 4 storage class specifiers can be split into 2 storage durations: automatic storage duration static storage duration

Automatic Storage Duration Variables with automatic storage duration are created when the block in which they are declared is entered. They exist while the block is active. They are destroyed when the block is exited. Keywords auto and register are used to declare variables of automatic storage duration. Only variables can have automatic storage duration. Example: Function’s local variables. Local variables have automatic storage duration by default.

Static Storage Duration Static storage duration – Identifiers exist from the point at which the program begins execution. extern – Global variables and function names Global variables are created by placing the declarations outside any function definition. They retain their values through out the execution of the program. static - Static variables retain their value when the function is exited. static int number = 1; Static local variables are known only in the function in which they are defined.