Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Chapter Five Functions
/* program to find area of a ring */ #include int main() { float a1,a2,a,r1,r2; printf("Enter the radius : "); scanf("%f",&r1); a1 = 3.14*r1*r1; printf("Enter.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
TK1913-C Programming1 TK1913-C Programming 1 C Library Functions C provides a collection of library functions for programmers If these library functions.
CS 201 Functions Debzani Deb.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Computer Science 210 Computer Organization Introduction to C.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
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.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Functions Functions, locals, parameters, and separate compilation.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Computer Science 210 Computer Organization
Introduction to Programming
C Programming Tutorial – Part I
Functions Separate Compilation
Chapter 5 Functions DDC 2133 Programming II.
Module 4 Functions – function definition and function prototype.
Programming Fundamentals Lecture #7 Functions
Functions in C Mrs. Chitra M. Gaikwad.
CSCI 161: Introduction to Programming Function
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Science 210 Computer Organization
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Chapter 5 Function Basics
Preprocessor.
Functions, Part 2 of 3 Topics Functions That Return a Value
Comments, Prototypes, Headers & Multiple Source Files
Function “Inputs and Outputs”
In C Programming Language
C Programming Language
What Is? function predefined, programmer-defined
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Lecture-3 Functions and Recursion

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

Example #include #define PI 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; }

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

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 program func.cfunc.c

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”

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

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

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

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 of recursionrecursion

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.

Homework 3 Follow this link to Homework 3Homework 3