Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lec8.

Similar presentations


Presentation on theme: "Lec8."— Presentation transcript:

1 lec8

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

3 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.

4 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

5 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

6 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.

7 Top-Down Design

8 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

9 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.

10 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.

11 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);

12 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();

13 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(" *** ");}

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

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

16 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

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

18 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

19 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();}

20 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();

21 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.

22 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) {...}

23 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) {...}

24 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.

25 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

26 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

27 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

28 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);

29 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);

30 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);

31 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)}

32 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

33 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);}

34 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

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


Download ppt "Lec8."

Similar presentations


Ads by Google