1 / 35

Introduction to Pre-defined and User-defined Functions in C

Learn about pre-defined and user-defined functions in C programming. Understand how functions are used to perform specific tasks and the advantages of writing functions. Explore the concept of code reuse and the process of top-down design.

eliason
Download Presentation

Introduction to Pre-defined and User-defined Functions in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 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. Pre-defined and User-defined Functions

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

  5. 3. Program doesn’t sends data to the function and receivesdata 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 Types of Functions

  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. 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) 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. Syntax (function without arguments) ftypefname (void); Example void drawcircle(void);

  12. calls function drawcircle and causes it to begin execution 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 Drawcircle();

  13. 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) 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} Example void drawcircle(void) {printf(" *** "); printf("* *"); printf(" *** ");}

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

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

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

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

  18. continue 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");}

  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 y=sqrt(x); #include<math.h> Pre-Defined Standard Lib y=abs(x); #include<stdlib.h> 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_typefunction_name (formal parameter list); Call:variable_name = function_name (actual argument list); Definition:return_typefunction_name (formal parameter list) {...} 2. Program sends data to the function and doesn’t receive data from the function. Prototype:voidfunction_name (formal parameter list); Call: function_name (actual argument list); Definition: voidfunction_name (formal parameter list) {...}

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

  24. Argument List Correspondence 1. The number of actual arguments used ina function call must be the same as the number of the formal parameters listed in the functionprototype 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. What is sent to the function What is returned from the function (i.e. result) What is returned from the function (i.e. result) What is sent to the function Example Prototype& definition: floatsquared (int x, float y) Call: y = squared (x,y)

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

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

  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. continue Example #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);}

  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 Local Declaration 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) { /* local declarations */ } Functions 3

More Related