This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Slides:



Advertisements
Similar presentations
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Advertisements

Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
CS 201 Functions Debzani Deb.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CS 201 Functions Debzani Deb.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
King Saud University College of applied studies and community services CSC 1101 Computer Programming I Lecture 2.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
User defined functions
Oct 2001ANSI C Under Unix (v1.0)1 UNIX C Programming under Unix written and presented by M.T.Stanhope.
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.
1 ICS103 Programming in C Lecture 8: 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.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
presented BY : DURGESH KKHANDEKAR 1st semester
© 2016 Pearson Education, Ltd. All rights reserved.
Functions, Part 2 of 2 Topics Functions That Return a Value
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Functions in C Mrs. Chitra M. Gaikwad.
Lecture2.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
User Defined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
Functions, Part 2 of 3 Topics Functions That Return a Value
Lecture4.
Functions, Part 1 of 3 Topics Using Predefined Functions
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

User-Defined Functions Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 This lesson discusses the purpose and syntax of user-defined functions in ANSI C: Vocabulary: body calling function header function call library function prototype user-defined function

A function is … A named group of statements that can be called and evaluated and can return a value to the calling statement. ( 7.0/html/glossary/czgf.htm) A named group of statements in a program that perform a task when called on. ( A subprogram which returns a value of a specified type which is invoked as part of an expression. (

A programmer can create new customized functions. A new function that is not part of the library and is written by the programmer, it is called a user-defined function (UDF). One user-defined function that you have written is main() A library (or built-in) function is one that is included as part of the language. In C, library functions are defined in the header files (stdio.h, math.h, ctype.h, etc). Some library functions that you have used are: printf(), scanf(), getchar(). User-defined functions other than main() are sometimes called subfunctions. Functions are not a new concept. You have been using functions in every program in this course.

User Defined Functions in ANSI C A user defined function is a collection of code written by the programmer that can be called as a unit from within another function in the program. Functions serve several purposes: 1.Eliminate repetitious code 2.Improve modularization which a)Eases understanding for humans b)Simplifies maintenance c)Increases portability

Final grades are based on the average of 2 midterms and a final. The midterms are each worth 25% of the final grade and the final exam is worth 50%. Example: Test 1 84/ *.84 = 0.21 Test 2 80/ *.80 = 0.20 Final 90/ *.90 = 0.45 = 0.86 Consider how a user-defined function can be used to solve the following problem.

printf("Enter Score on Test 1: "); scanf("\n%lf", &Test1Score); printf("Enter points possible on Test 1: "); scanf("\n%lf", &Test1Possible); Test1Percent = Test1Score / Test1Possible; printf("Enter Score on Test 2: "); scanf("\n%lf", &Test2Score); printf("Enter points possible on Test 2: "); scanf("\n%lf", &Test2Possible); Test2Percent = Test2Score / Test2Possible; printf("Enter Score on Final Exam: "); scanf("\n%lf", &FinalScore); printf("Enter points possible on Final Exam: "); scanf("\n%lf", &FinalPossible); FinalPercent = FinalScore / FinalPossible; OverallScore = 0.25 * Test1Percent * Test2Percent * FinalPercent; printf("\nThe overall score is %5.2f", OverallScore; int main() { double Test1Score, Test1Possible; double Test2Score, Test2Possible; double Test3Score, Test3Possible; return 0; { Example: Click through to become familiar with the following flowchart and code snippet.

There are 3 steps to coding and using a function: Definition Prototype Call

The definition of a function is that part of the source code where the programmer lists the instructions that will be executed when the function is invoked. The definition comes AFTER main(); int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } DEFINITION

The prototype of a function is an outline of the function provided to the compiler. The prototype comes BEFORE main() int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } int CalcArea( int Width, int Length) ; PROTOTYPE DEFINITION

The function call is a line of code in one function that invokes another (or the same) function. int CalcArea( int Width, int Length) { int Area; Area = Width * Length; return Area; } int CalcArea( int Width, int Length) ; PROTOTYPE DEFINITION CALL int main() { int Side1; int Side2; int SquareFootage; scanf("\n%d %d",&Side1, &Side2); SquareFootage = CalcArea(Side1, Side2); printf("\nTotal Square Feet is: %d", SquareFootage); return 0; }

What flowcharting symbol is used to represent a function call? How many values can a function return?0 or 1 What flowcharting symbol is used to represent a returned value? What flowcharting symbol is used when there is more text than can fit into a regular shape? After all steps in a function are completed, what is the next instruction executed? The first instruction following the function call.