Functions.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

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.
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.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 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.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
© 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.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
© 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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
TMC1414/TMC1413 I NTRODUCTION T O P ROGRAMMING Lecture 06 Function.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
Dale Roberts CSCI 230 Functions Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Function – I What is a function Why we need functions?
5 C Functions.
Functions Course conducted by: Md.Raihan ul Masood
Dr. Shady Yehia Elmashad
5.13 Recursion Recursive functions Functions that call themselves
5 C Functions.
IS Program Design and Software Tools Introduction to C++ Programming
C Functions -Continue…-.
5 C Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
JavaScript: Functions
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
CS1061 C Prgramming Lecture 11: Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Functions in C.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
5 C Functions.
5 C Functions.
Function.
1-6 Midterm Review.
Function.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Functions

Introduction Divide and conquer Construct a program from smaller pieces or components These smaller pieces are called modules Each piece more manageable than the original program

Program Modules in C Functions Function calls Invoking functions Programs combine user-defined functions with library functions C standard library has a wide variety of functions Function calls Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results

Program Modules in C Fig. 5.1 Hierarchical boss function/worker function relationship.

Functions Functions allow us to group commonly used code into a compact unit that can be used repeatedly We have already encountered one function, main. It is a special function called at the beginning of the program. All other functions are directly or indirectly called from main Suppose we want to write a program to compute the area of three triangles. We could write out the formula three times, or we could create a function to do the work

Cont … The function proper begins with the line: float triangle(float width, float height) float is the function type. The two parameters are width and height. They are of type float also. C uses a form of parameter passing called "Call by value". When our procedure triangle is called, with code such as: triangle(1.3, 8.3); The function computes the area with the statement: area = width * height / 2.0; What's left is to give the result to the caller. This step is done with the return statement: return (area);

The code float triangle(float width, float height) { float area; /* Area of the triangle */ area = width * height / 2.0; return (area); }

Calling the function size = triangle(1.3, 8.3); is a call to the function triangle. C assigns 1.3 to the parameter width and 8.3 to height. The return value of this function is 5.4, so our statement: size = triangle (1.3, 8.3) assigns size the value 5.4.

printf("Triangle #1 %f\n", triangle(1.3, 8.3)); Whole code float triangle(float width, float height) { float area; /* Area of the triangle */ area = width * height / 2.0; return (area); } int main() { printf("Triangle #1 %f\n", triangle(1.3, 8.3)); printf("Triangle #2 %f\n", triangle(4.8, 9.8)); printf("Triangle #3 %f\n", triangle(1.2, 2.0)); return (0); }

Using a function If we want to use a function before we define it, we must declare it just like a variable to inform the compiler about the function. We use the declaration: float triangle (float width, float height); for the triangle function. This declaration is called the function prototype. The variable names are not required when declaring a function prototype. Our prototype could have just as easily been written as: float triangle(float, float);

int main(int numParms, char *parms[]) { int result, k; printf(“Enter a number ); Scanf(“%d”, &k) result = sum(k); printf("The sum is %d\n", result); return 0; } int sum(int n) int count; int result; result = 0; for (count=1; count<=n; count++) result += count; return result;

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

Math Library Functions

5.4 Functions Functions Benefits of functions Modularize a program All variables defined inside functions are local variables Known only in function defined Parameters Communicate information between functions Local variables Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition

5.5 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 – indicates that the function returns nothing Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter unless, the parameter is of type int

Function Definitions Function definition format (continued) return-value-type function-name( parameter-list ) { declarations and statements } Definitions and statements: function body (block) Variables can be defined inside blocks (can be nested) Functions can not be defined inside other functions Returning control If nothing returned return; or, until reaches right brace If something returned return expression;

fig05_03.c (Part 1 of 2)

fig05_04.c (Part 1 of 2)

Function Prototypes Function prototype Promotion rules and conversions Function name 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 The function with the prototype int maximum( int x, int y, int z ); Takes in 3 ints Returns an int Promotion rules and conversions Converting to lower types can lead to errors

5.6 Function Prototypes

Header Files Header files Custom 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

5.7 Header Files

5.8 Calling Functions: Call by Value and Call by Reference 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 Call by reference Passes original argument Changes in function effect original Only used with trusted functions For now, we focus on call by value

5.13 Recursion Recursive functions Functions that call themselves Can only solve a base case Divide a problem up into What it can do What it cannot do What it cannot do resembles original problem The function launches a new copy of itself (recursion step) to solve what it cannot do Eventually base case gets solved Gets plugged in, works its way up and solves whole problem

5.13 Recursion Example: factorials Notice that 5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! 4! = 4 * 3! ... Can compute factorials recursively Solve base case (1! = 0! = 1) then plug in 2! = 2 * 1! = 2 * 1 = 2; 3! = 3 * 2! = 3 * 2 = 6;

5.13 Recursion

fig05_14.c (Part 1 of 2)

1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320 9! = 362880 10! = 3628800 fig05_14.c (Part 2 of 2)

5.14 Example Using Recursion: The Fibonacci Series Each number is the sum of the previous two Can be solved recursively: fib( n ) = fib( n - 1 ) + fib( n – 2 ) Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1) + fibonacci( n – 2 ); }  

5.14 Example Using Recursion: The Fibonacci Series function fibonacci f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +  

fig05_15.c (Part 1 of 2)

5.14 Example Using Recursion: The Fibonacci Series

Recursion vs. Iteration Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case recognized Both can have infinite loops Balance Choice between performance (iteration) and good software engineering (recursion)