CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.

Slides:



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

Introduction to C Programming
Spring Semester 2013 Lecture 5
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Introduction to Computer Programming in c
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Computer programming Lectures 6&7. Lectures 6&7: Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
CPS120: Introduction to Computer Science Functions.
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.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Computer programming Lectures 6&7. Lectures 6&7: Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
CCSA 221 Programming in C CHAPTER 8 WORKING WITH FUNCTIONS ALHANOUF ALAMR 1.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
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.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
User-Written Functions
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Computer programming Lecture 3.
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 42 Topics Functions That Return a Value
CPS125.
Presentation transcript:

CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1

Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local Variables –Returning Function Results –Declaring a Function Prototype –Functions and Arrays Arrays as parameters Sorting Arrays Multidimensional Arrays –Global Variables –Automatic and Static Variables –Recursive Functions 2

What is a function A function in C: is a self-contained unit of program code designed to accomplish a particular task. The concept has some equivalent in all high-level programming languages: functions, subroutines, and procedures The use of a function: a "black box" –defined in terms of the information that goes in (its input) and the value or action it produces (its output). –what goes on inside the black box is not your concern, unless you are the one who has to write the function. –Think on how you used functions printf, scanf, getchar ! What kind of “output” comes out from a function black box ? –Some functions find a value for a program to use. Example: getchar() returns to the program the next character from the standard input buffer. –Some functions cause an action to take place. Example: printf() causes data to be printed on the screen –In general, a function can both produce actions and provide values. 3

Defining a function #include void printMessage (void) { printf ("Programming is fun.\n"); } int main (void) { printMessage (); return 0; } Function Definition -occurs ONE time for all -outside other functions Function calls (invocations) -occurs ANY (0-N) times -statement inside (other) functions body 4

Transfer of control flow main printMesage printf When a function call is executed, program execution is transferred directly to the indicated function. After the called routine is finished (as signaled by the closing brace) the program returns to the calling routine, where program execution continues at the point where the function call was executed. { { { } } 5

Function definitions return-type function-name(argument declarations) { declarations and statements } void printMessage ( void ) { printf ("Programming is fun.\n"); } General form of function definition: return-typearguments 6

Function prototype The first line of the function definition Contains everything that others (other functions) need to know about the function in order to use it (call it) void printMessage (void) void calculateTriangularNumber (int n) return-type function-name(argument declarations) { declarations and statements } Function prototype 7

Function arguments arguments (parameters): a kind of input for the function blackbox In the function definition: formal arguments (formal parameters) –Formal parameter: a name that is used inside the function body to refer to its argument In the function call: actual arguments (actual parameters) –The actual arguments are values are assigned to the corresponding formal parameters. –The actual argument can be a constant, a variable, or an even more elaborate expression. –The actual argument is evaluated, and its value is copied to the corresponding formal parameter for the function. Because the called function works with data copied from the calling function, the original data in the calling function is protected from whatever manipulations the called function applies to the copies. 8

Example: arguments // Function to calculate the nth triangular number #include void calculateTriangularNumber ( int n ) { int i, triangularNumber = 0; for ( i = 1; i <= n; ++i ) triangularNumber += i; printf ("Triangular number %i is %i\n", n, triangularNumber); } int main (void) { calculateTriangularNumber (10); calculateTriangularNumber (20); calculateTriangularNumber (50); return 0; } formal argument actual argument local variables 9

Automatic local variables main calculateTriangularNumber { { } n i triangularNb 10

Example: scope of local variables #include void f1 (float x) { int n=6; printf(“%f \n”, x+n); } int f2(void) { float n=10; printf(“%f \n”,n); } int main (void) { int n=5; f1(3); f2(); return 0; } 11

Example: arguments #include void gcd (int u, int v) { int temp; printf ("The gcd of %i and %i is ", u, v); while ( v != 0 ) { temp = u % v; u = v; v = temp; } printf ("%i\n", u); } int main (void) { gcd (150, 35); gcd (1026, 405); gcd (83, 240); return 0; } 12

Example: arguments are passed by copying values ! #include void gcd (int u, int v) { int temp; printf ("The gcd of %i and %i is ", u, v); while ( v != 0 ) { temp = u % v; u = v; v = temp; } printf ("%i\n", u); } int main (void) { int x=10,y=15; gcd (x, y); printf(“x=%i y=%i \n”,x,y); return 0; } The actual parameters x and y are not changed ! The formal parameters u and v are assigned new values in the function 13

Example: arguments are passed by copying values ! #include void multiplyBy2 (float x) { printf(“parameter at start: %.2f, at %p \n”,x, &x); x*=2; printf(“parameter at end: %.2f, at %p \n”,x, &x); } int main (void) { float y = 7; printf (“y before call: %.2f, at %p \n", y, &y); multiplyBy2 (y); printf (“y after call: %.2f, at %p \n", y, &y); return 0; } 14

Arguments by copying main multiplyBy2 { { } 7 y x

Returning function results A function in C can optionally return a single value return expression; The value of expression is returned to the calling function. If the type of expression does not agree with the return type declared in the function declaration, its value is automatically converted to the declared type before it is returned. A simpler format for declaring the return statement is as follows: return; Execution of the simple return statement causes program execution to be immediately returned to the calling function.This format can only be used to return from a function that does not return a value. If execution proceeds to the end of a function and a return statement is not encountered, it returns as if a return statement of this form had been executed. Therefore, in such a case, no value is returned. If the declaration of the type returned by a function is omitted, the C compiler assumes that the function returns an int ! 16

Return example void printMessage (void) { printf ("Programming is fun.\n"); return; } 17

Example: function result /* Function to find the greatest common divisor of two nonnegative integer values and to return the result */ #include int gcd (int u, int v) { int temp; while ( v != 0 ) { temp = u % v; u = v; v = temp; } return u; } int main (void) { int result; result = gcd (150, 35); printf ("The gcd of 150 and 35 is %i\n", result); result = gcd (1026, 405); printf ("The gcd of 1026 and 405 is %i\n", result); printf ("The gcd of 83 and 240 is %i\n", gcd (83, 240)); return 0; } 18