1 / 11

Chapter 7: Functions

Chapter 7: Functions. Dr. Ameer Ali. Overview. A function is a self contained program segment that will carry some well defined and specific task A function will carry over its intended actions whenever it is accesses from some other portion of the program

duane
Download Presentation

Chapter 7: Functions

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. Chapter 7: Functions Dr. Ameer Ali

  2. Overview • A function is a self contained program segment that will carry some well defined and specific task • A function will carry over its intended actions whenever it is accesses from some other portion of the program • The same function can be accessed from several places within the program

  3. Declaration void Name (void) { Statement; } Return_type Name (type_of_parameter Name_of_Parameter) { Statements; } Return_type Name (agr1, arg2, …, agrn) { Statements; }

  4. Example • Lower case to upper case conversion #include<stdio.h> char lower_to_Upper(char c1) { char c2; c2=toupper(c1); return c2; } Void main(void) {char upper, lower; printf(“Enter your lower case letter”); scanf(“%c”,&lower); upper=lower-to_upper(lower); printf(“\n Your upper case char is %c”, upper); getch(); }

  5. Accessing a function • Must be accessed in the similar way upper=lower-to_upper(lower);

  6. Function Prototype • Lower case to upper case conversion #include<stdio.h> long int factorial(int n) { int i; long int prod; prod=1; if (n>0) { for(i=1;i<=n;i++) { prod*=i; } } return prod; }

  7. Function Prototype Void main(void) {int n; long int fact; printf(“Enter your number”); scanf(“%d”,&n); fact=factorial(n); printf(“\n Factorial of n=%ld”, fact); getch(); }

  8. Function Prototype #include<stdio.h> long int factorial(int n); //function prototype void main(void) {int n; long int fact; printf(“Enter your number”); scanf(“%d”,&n); fact=factorial(n); printf(“\n Factorial of n=%ld”, fact); getch(); }

  9. long int factorial(int n) { int i; long int prod; prod=1; if (n>0) { for(i=2;i<=n;i++) { prod*=i; } } return prod; }

  10. Passing Arguments

  11. Recursion

More Related