CS1061 C Prgramming Lecture 11: Functions

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
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.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
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.
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.
© 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.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
CPS120: Introduction to Computer Science Functions.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
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.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
© 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.
1 Lecture 3 Part 2 Storage Classes, Scope, and Recursion.
C++ Programming Lecture 12 Functions – Part IV
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.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
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.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
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
User-Written Functions
Chapter 6 - Functions modular programming general function format
IS Program Design and Software Tools Introduction to C++ Programming
Programming Fundamentals Lecture #7 Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Deitel- C:How to Program (5ed)
CSCI 161: Introduction to Programming Function
Functions in C.
Programmazione I a.a. 2017/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Formatted and Unformatted Input/Output Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Introduction to Functions
Chapter 4 void Functions
Chapter 6 - Functions Outline 5.1 Introduction
Functions Recursion CSCI 230
Introduction to Classes and Objects
Function.
Functions, Part 1 of 3 Topics Using Predefined Functions
1-6 Midterm Review.
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
Function.
CPS125.
Classes and Objects Systems Programming.
Presentation transcript:

CS1061 C Prgramming Lecture 11: Functions A. O’Riordan, 2004

Functions Functions modularize a program All variables declared inside functions are local variables known only in the 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

Functions (2) Every C program has to have at least one function called main(). This is the first function that is executed. This function may then call other functions. It is conventional to place the main() function at the start of the program followed by all the other functions in the file.   It is universally considered good software engineering practice to keep functions short. Long functions can be difficult to understand. A rule of thumb is to keep functions shorter than 50 lines, except in exceptional circumstances.

Form of a Function A function has the form:   return_type function_name (<parameters>){ <local variable declarations> <statements> } Firstly we have the function name (and return type and parameters). Note that even if a function returns a value, the caller does not have to use it. Any C function may have parameters, which is a mechanism for passing extra information to a function. Then you have the body of the function enclosed by curly braces {}. In the body you can have zero, one or more local variable declarations, followed by zero, one or more statements.

Example Program with Two Functions Here is an example where main() calls another function printMessage(). #include <stdio.h> void printMessage(); /* prototype */ int main(){ printMessage(); } void printMessage(){ printf("Function has been called\n"); return;

Prototypes Functions need to be declared before they are defined, printMessage() is declared in the second line of the program above The declaration informs the compiler about the details of the function, such as the function name, the return type and the number and type of each argument. In C, function declarations are called prototypes. C is case sensitive so a function called MyFunction is different than a function called myfunction. The return type is the data type of the result (default int). Use void to indicate that a function returns nothing.

Functions with Parameters (2) Most real function take parameters (sometimes called formal parameters). The values that are supplied to these parameters are called arguments (or actual arguments). The name of a function together with its parameter list is called its signature. A parameter list consists of comma-separated variables. Types for each variable must be specified. The number and order of variables is important, e.g. passing an incorrect argument list, such as passing an argument of the wrong type, will be caught by the compiler. In function prototypes variable names may be omitted.

Functions with Parameters (2) Here is a function that accepts three parameters, the first an int, the second a float and the third a char. void myFunction( int a, float b, char c) { printf("The parameters are %d, %f, %c\n", a, b, c); } Valid calls to our function above could look like any of the following (assuming x is an int with value 10, y is a float with value 10.0 and z is a character with value ‘F’). All three calls below pass the same values. myFunction( x, y, z ); myFunction( 10, 10.0, ‘F' ); myFunction( x, x, 70 ); /* x implicitly cast to a float, 70 is ASCII for ‘F’ */

Returning a Value #include<stdio.h> typedef int max_speed; max_speed get_faster(max_speed, max_speed); int main(){ max_speed bmw=240, subaru=230; printf("The faster car can go %dkph\n", get_faster(bmw,subaru)); } max_speed get_faster(max_speed car1, max_speed car2){ if (car1 >= car2) return(car1); return(car2);

Function with Multiple Returns It is possible for a function to have multiple return statements. int validate_input(char operator) { switch(operator){ case ‘+’: case ‘-‘: return 1; case ‘*’: case ‘/’: return 2; default: return 0; }

Recursion Recursive functions - functions that call themselves Example: factorials 5! = 5 * 4 * 3 * 2 * 1 Notice that 5! = 5 * 4! so we can compute factorials recursively Choice between performance (iteration) and elegance (recursion) Recursion v. Iteration Repetition Iteration: explicit loop Recursion: repeated function calls Termination Iteration: loop condition fails Recursion: base case recognized

Recursion Example A recursive function calls itself either directly or indirectly. The factorial function can be written as a recursive function:   int fac(int n){ if (n == 1) return 1; return (n * fac(n-1)); } A recursive function must always have a stopping condition, otherwise execution goes on infinitely. In the fac() example, the stopping condition is when n is equal to 1. The fact that each successive call to fac() has a smaller argument ensures convergence to the stopping condition.