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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Spring Semester 2013 Lecture 5
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Lecture 2 Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 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.
Structure of a C program
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Guide To UNIX Using Linux Third Edition
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Chapter 6: Functions.
A First Book of ANSI C Fourth Edition
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Basic Program Construction
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
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. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
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
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
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.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
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.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Value-Returning Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
User-Defined Functions
Chapter 2 – Getting Started
Introduction to C++ Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 6: User-Defined Functions I
Introduction to C Topics Compilation Using the gcc Compiler
CPS125.
Presentation transcript:

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. 3. Every C program ends with an END word. 4. main() is where the program begins its execution. 5. A line in a program may have more than one statement. 6. A printf statement can generate only one line of output. 7. The closing brace ‘}’ of the main() in a program is the logical end of the program. 8. The purpose of the header file such as stdio.h is to store the source code of a program. 9. Comments cause the computer to print the next enclosed between /* and */ when executed. 10. Syntax errors will be detected by the compiler.

Midterm questions (11-20) review 11. Every C program must have at least one user-defined function. 12. Only one function may be named main(). 13. Use of comments reduces the speed of execution. 14. Comments serve as internal documentation for programmers. 15. A comment can be inserted in the middle of a statement 16. In C, we can have comments inside comments. 17. Any valid printable ASCII character can be used in an identifier. 18. All variables must be given a type when they are declared. 19. Declarations can appear anywhere in a program. 20. C treats the variables name and Name to be same.

Reviewing Functions What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. printf() causes data to be printed on the screen. Why should you use functions? They save you from repetitious programming (save time and space) Using a function is worthwhile because it makes a program more modular, hence easier to read and easier to change or fix Main program Function AFunction BFunction C B1B2

Example Suppose, for example, that you want to write a program that does the following: Read in a list of numbers Sort the numbers Find their average Print a bar graph A function can be thought as a "black box" defined in terms of the information that goes in (its input) and the value or action it produces (its output).

Elements of user-defined functions Both function names and variables are considered identifies and therefore they must adhere to the rules for identifiers. Like variables, functions have types (such as int) associated with them. Like variables, function names and their types must be declared and defined before they are used in a program

What do you need to know about functions? You need to know how to define them properly How to call them up for use. And how to set up communication between functions. The specifier %s is used to print strings String constants

Definition of functions Function definition include function name; function type; list of parameters; local variable declarations function statements a return statement All the six elements are grouped into two parts, Function header (first three elements); Function body (second three elements);

Function Arguments

Defining a Function with an Argument: Formal Parameters The function definition begins with the following ANSI C function header: You can't use a list of variables of the same type

Prototyping and Calling a Function with Arguments We can omit variable names in the prototype: Declaration on an ANSI function prototype: We give ch and num values by using actual arguments in the function call. Consider the first use of show_n_char(). The actual argument can be a constant, a variable, or an even more elaborate expression

The Black-Box Viewpoint Taking a black-box viewpoint of show_n_char(), the input is the character to be displayed and the number of times to be repeated. The input is communicated to the function via arguments. This information is enough to tell you how to use the function in main(). The fact that ch, num, and count are local variables private to the show_n_char() function is an essential aspect of the black box approach. If you were to use variables with the same names in main(), they would be separate, independent variables.

Returning a Value from a Function with return The function returns the value that was assigned to min Functions can be used as part of an expression

Function Types Functions should be declared by type. A function with a return value should be declared the same type as the return value. Functions with no return value should be declared as type void. the following function heading indicates that a defined function takes two type int arguments but that returns a type double value.

Function prototypes All identifiers in C need to be declared before they are used. For functions the declaration needs to be before the first call of the function. A full declaration (prototype) includes the return type and the number and type of the arguments. Having the prototype available before the first use of the function allows the compiler to check that the correct number and type of arguments are used in the function call and that the returned value, if any, is being used reasonably.

Function prototypes prototype With function prototype Without function prototype

Recursion C permits a function to call itself. This process is termed recursion. Recursion often can be used where loops can be used. Note that each level of recursion uses its own private n variable It's vital that a recursive function contain something to halt the sequence of recursive calls

Tail Recursion Tail recursion acts like a loop. The recursive call is at the end of the function, just before the return statement. n! = n x (n-1)! Although the recursive call to rfact() is not the last line in the function, it is the last statement executed when n > 0, so it is tail recursion.

Recursion and Reversal Write a function that prints the binary equivalent of an integer. putchar( r ? '1' : '0');

Recursion Pros and Cons + recursion offers the simplest solution to some programming problems. - some recursive algorithms can rapidly exhaust a computer's memory resources Example: Suppose you use the function call Fibonacci(40). It allocates a variable called n and evokes Fibonacci() twice, creating two more variables called n at the second level of recursion. Each of those two calls generates two more calls, requiring four more variables called n at the third level of recursion, for a total of seven variables. This processes results in a enormous number of allocated variables.