1 / 12

Functions and Recursion in C Preprocessor - Lecture 3

This lecture covers topics such as functions, macros, conditional compilations, prototypes, variable scope, recursion, and more in the C programming language.

mcannella
Download Presentation

Functions and Recursion in C Preprocessor - Lecture 3

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. Lecture-3 Functions and Recursion

  2. C Preprocessor • Includes header files like stdio.h • Expands macros defined • Handles conditional compilations prog.c Preprocessor Processor a.out

  3. Example #include <stdio.h> #define PI 3.142 int main() { float rad=0.0; printf(“Enter the radius of Circle: “); scanf(“%f”, &rad); #ifdef chk_rad if(rad<0) { printf(“Error!\n”); return 1;} #endif printf(“Area of Circle= %f”, PI*rad*rad); return 0; }

  4. Functions • “Subprograms” with parameters, program statements, and a result • Break code into simpler pieces • Promote “reuse” of code in both the same and other programs • Link to code written in other programming languages

  5. Prototypes and Definitions • Prototypes tell the compiler to expect a function with the specified signature • Definition also includes the body of the function • May have more than one parameter Follow this link to the programfunc.c

  6. Miscellaneous • Libraries included in the C programming environment are a collection of header files, e.g. stdio.h, math.c, etc. • Functions not taking any parameters should use keyword “void” in the parameter list • Functions with no return type should use return type of “void”

  7. Variable scope • Only one function is active in the memory at any time • Thus it may use only those variables that it declares • Functions can re-declare the same variables within its scope

  8. In-class exercise 3-1 • Write a program which finds the largest number from three numbers given to it • The main function will prompt the user for three numbers and pass them onto a function which computed the largest • The function will return the largest number to the main, which will then print out the result

  9. Call by Value • C uses “call by value” to pass parameters between functions by duplicating the values • Changing the originally passed parameter in the calling function, does not change the variable value in the called function

  10. Recursion • Recursion is when the function calls itself , either directly or indirectly • Each recursion has a “base case” which will make the recursion stop. • Some conditional statement, like an “if-else” is required to test for the base case for the recursion to stop Follow this link to an example ofrecursion

  11. In-class exercise 3-2 • Create a program which computes the Greatest Common Divisor for given two numbers • Use the method of eigen values • Pass the original values to a function • The function computes (num1%num2) and recursively calls itself with num2, and the remainder as parameters till the remainder becomes zero(base case!). Then it returns the first parameter as the GCD.

  12. Homework 3 Follow this link to Homework 3

More Related