Lec8.

Slides:



Advertisements
Similar presentations
Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Advertisements

COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
CS 201 Functions Debzani Deb.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Chapter 6: User-Defined Functions I
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.
ICS103 Programming in C Lecture 9: Functions I
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
1 Introduction to Computers II Lecture 4 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
chap3 Chapter 3 Top-Down Design with Functions.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
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.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
EKT120: Computer Programming
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
User-Written Functions
Chapter 6: User-Defined Functions I
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Lesson #6 Modular Programming and Functions.
Function Topic 4.
Lesson #6 Modular Programming and Functions.
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Functions I Creating a programming with small logical units of code.
Functions Chapter 3 of the text Motivation:
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
1) C program development 2) Selection structure
Lecture4.
A function with one argument
Lecture3.
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Introduction to Problem Solving and Programming
Functions Extra Examples.
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Functions, Part 1 of 3 Topics Using Predefined Functions
Top-Down Design with Functions
Functions I Creating a programming with small logical units of code.
CPS125.
Top-Down Design with Functions
Computer Science II CS132/601* Lecture #C-1.
Presentation transcript:

lec8

Introduction A function is a subprogram that performs a specific task. Functions you know: printf("Hi"); scanf("%i",&number); Y = sqrt(x);

Pre-defined and User-defined Functions Pre-defined Function Is a function that is already defined in one of the many C libraries. It is included in the program through the preprocessor directive statement #include< > and used in the program to perform a specific task. Ex: #include<stdio.h> Defines printf & scanf functions User-defined Function Is a function that is created by the user. It is defined before the main program through a function prototype and called upon in the main program to perform a specific task.

Types of Functions 1. Program sends data to the function and receives data from the function. Ex. y=sqrt(x); x y sqrt 2. Program sends data to the function and doesn’t receive data from the function. Ex. Printf("Hi"); "Hi" printf

Types of Functions 3. Program doesn’t sends data to the function and receives data from the function. Ex. Number = Get_Number(); Get_Number Number 4. Program doesn’t sends data to the function and doesn’t receive data from the function. Ex. Display_Comment(); Display_Comment

Top-Down Design Often the algorithms needed to solve a problem is more complex than those we have seen so far. Often we must break up the problem into subproblems to develop the program solution. In attempting to solve a subproblem at one level, we introduce new subproblems at lower levels. This process is called top-down design.

Top-Down Design

Top-Down Design One way that we can implement top-down design in our programs is by defining our own functions. Often, we will write one function subprogram for each subproblem in the structured chart

Advantages of Writing Functions 1. Decreases the main program’s length and reduces the chance to make errors. 2. Makes it easier to define programming tasks between programmers. Each programmer can be responsible for a particular set of functions. 3. Allows procedural abstraction; a programming technique in which a main function consists of a sequence of function calls and each function is implemented separately. This helps to focus on one function at a time instead of writing the complete program all at once. 4. Allows code reuse of function programs.

Code Reuse Reusing program fragments that have already been written and tested whenever possible. It is a way to help in writing error free code.

User-defined Functions Function Prototype A way to declare a function. This tells the compiler: The name of the function The data type of received data(if there is). The type & name of sent data (if there is). Also called the parameters of the function. defines a function called drawcircle with not sent nor received data (when the program doesn’t send to the function and doesn’t receive from the function, the function is called a void function) Syntax (function without arguments) ftype fname (void); Example void drawcircle(void);

User-defined Functions Function Call A function call transfers control from the main program to the function or subprogram. Syntax (function without arguments) fname (); Example calls function drawcircle and causes it to begin execution Drawcircle();

User-defined Functions Function Definition Defines the function operation by writing the code for the function in a similar way to writing the code for the main program. Syntax (function without arguments) Ftype fname(void) {local declaration executable statements} does not contain the return statement because this function will not send any data to the program “main function” (i.e. a function should contain the return statement only if it is going to send data to the program) Example void drawcircle(void) {printf(" *** "); printf("* *"); printf(" *** ");}

User-defined Functions Main Program void drawcircle (void); int main(void) { drawcircle(); } void drawcircle (void) Function Prototype Function Call Function Definition

User-defined Functions Main Program void drawcircle (void); void drawcircle (void) { } int main(void) drawcircle(); Function Prototype Function Definition Function Call

User-defined Functions Order of Execution void function1 (void); void function2 (void); int main(void) { function1(); function2(); } void function1 (void) { } void function2 (void) transfer of control to function1 transfer of control back to program transfer of control to function2 transfer of control back to program

Example - Drawing a Stick Figure * * * * * / \ / \ /____\ / \

Example - Drawing a Stick Figure /* draws a stick figure */ #include <stdio.h> void draw_circle(void); /* Function prototype */ void draw_intersect(void); /* Function prototype */ void draw_base(void); /* Function prototype */ void draw_triangle(void); /* Function prototype */ int main(void) {draw_circle(); /* function call */ draw_triangle(); /* function call */ draw_intersect(); /* function call */ return(0); } void draw_circle(void) /* Function definition */ {printf(" * \n"); printf("* *\n"); printf(" * * \n");} continue

Example - Drawing a Stick Figure - Continue /* draws a stick figure - continue*/ void draw_intersect(void) /* Function definition */ {printf(" /\\ \n"); printf(" / \\ \n"); printf("/ \\\n");} void draw_base(void) /* Function definition */ {printf("------\n");} void draw_triangle(void) /* Function definition */ {draw_intersect(); draw_base();}

Comparison of Various Functions Function Type Declaration/Prototype Call Definition Input/Output #include <stdio.h> printf("Hi"); Pre-Defined Mathematical #include<math.h> y=sqrt(x); Pre-Defined Standard Lib #include<stdlib.h> y=abs(x); Pre-Defined After the main program or prototype User-Defined void drawcircle(void) drawcircle();

Using User-defined Functions 1. Declare the function before the main program. 2. Call the function inside the main program. 3. Define the function after the main program.

Types of Functions 1. Program sends data to the function and receives data from the function. Prototype: return_type function_name (formal parameter list); Call: variable_name = function_name (actual argument list); Definition: return_type function_name (formal parameter list) {...} 2. Program sends data to the function and doesn’t receive data from the function. Prototype: void function_name (formal parameter list); Call: function_name (actual argument list); Definition: void function_name (formal parameter list) {...}

Types of Functions 3. Program doesn’t sends data to the function and receives data from the function. Prototype: return_type function_name (void); Call: variable_name = function_name (); Definition: return_type function_name (void) {...} 4. Program doesn’t sends data to the function and doesn’t receive data from the function. Prototype: void function_name (void); Call: function_name (); Definition: void function_name (void) {...}

Argument List Correspondence 1. The number of actual arguments used in a function call must be the same as the number of the formal parameters listed in the function prototype and definition. 2. The Order of the arguments in the lists determines correspondence and must be the same in arguments and parameters. The first actual argument corresponds to the first formal parameter, the second actual argument corresponds to the second formal parameter and so on. 3. The data types of the actual argument and the formal parameter must match.

Example Prototype& definition: float squared (int x, float y) What is returned from the function (i.e. result) What is sent to the function Call: y = squared (x,y) What is returned from the function (i.e. result) What is sent to the function

Examples of Function Prototype & definition float squared (int number) Returns back a float type to the main program An integer type called number is sent to the function squared void print_report (int report_number) Returns nothing to the main program An integer type called report_number is sent to the function print_report

Examples of Function Prototype & definition char get_menu_choice (void) Returns back a character type to the main program Nothing is sent to the function get_menu_choice void print_comment (void) Returns nothing to the main program Nothing is sent to the function print_comment

Program Example 1 /* computes half of the value of x */ .......... float half_of(float k); /* function prototype */ int main(void) { y=half_of(x); /* calling function */ } float half_of(float k) /* function definition */ k = k / 2; return(k);

Program Example 2 /* computes the cube of num */ .......... int cubed(int x); /* function prototype */ int main(void) { y=cubed(num); /* calling function */ } int cubed(int x) /* function definition */ return(x*x*x);

Program Example 3 /* displays the largest of two numbers */ .......... int larger(int x,int y); /* function prototype */ int main(void) { y=larger(num1,num2); /* calling function */ } int larger(int x,int y) /* function definition */ return(larger_num);

Program Example 4 /* calculates the area and circumferencce of a circle */ .......... float find_area(float r); /* function prototype */ float find_circum(float r); /* function prototype */ int main(void) { area=find_area(r); /* calling function */ circum = find_circum(r); /* calling function */ } float find_area(float r) /* function definition */ { return(PI*POW(r,2);} float find_circum(float r) {return(2.0*PI*r)}

Example continue #include<stdio.h> int menu (void); int main(void) {int n1, n2, choice; do{choice=menu(); if (choice==1 || choice==2) {printf("\nEnter two numbers: "); scanf("%i%i", &n1, &n2);} if (choice==1) printf("\n%i + %i = %i\n", n1, n2, n1+n2); else if (choice==2) printf("\n%i - %i = %i\n", n1, n2, n1-n2); else if (choice!=3) printf("\nInvalid choice... Try again...\n");} while (choice!=3); return(0);} continue

Example int menu (void) {int choice; printf("\n-----------------------\n"); printf("1. Add two numbers\n"); printf("2. Subtract two numbers\n"); printf("3. Quit\n"); printf("-----------------------\n\n"); printf("Please Enter your choice: "); scanf("%i", &choice); return(choice);}

Global and Local Declarations When the declaration is placed at the bigenning of the main function or sub-functions. Global Declaration When the declaration is placed immediately after the pre-processor directives. IMPORTANT: in the C programming language local variables have precedence over global variables. Functions 2

Global and Local Declarations #include <stdio.h> /* global declarations */ void function1(void); int main(void) { /* local declarations */ } void function1(void) Functions 3