FUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value
|
|
- Aileen Whitehead
- 6 years ago
- Views:
Transcription
1 FUNCTIONS Definition: A is a set of instructions under a name that carries out a specific task, assigned to it. CLASSIFICATION of s: 1. User defined s (UDF) 2. Library s USER DEFINED FUNCTIONS Without arguments With arguments Without return With return Without return With return void sum( ) int sum( ) void sum(int,int) int sum(int,int) Example: without arguments and without return value without arguments and with return value with arguments and without return value with arguments and with return value Advantages: 1. It avoids the need for redundant programming of the same instructions. 2. Logical clarity of the programming will be clear. 3. Easy to debug. 4. A library with user defined s can be created. Function prototype: If we use any variable in the program, we declare that variable in the declaration section of the program. Similarly any that is to be called should be declared before its call in the calling. This declaration of a is called prototype. Function declaration should be done either in the declaration section of calling or before it. The prototype should agree with definition of that. Prototype contains the data type returned by the followed by the name of that and a pair of parentheses enclosing the data type of each parameter separately. Optionally the name of each parameter may be given. Ex: int fact(int); or int fact(int n); If there are no parameters, void should be shown in parentheses. Similarly if there is no return value, return value type should be indicated as void. Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 27
2 FUNCTION CALL Function call means, accessing a. A can be called by specifying its name, followed by a list of arguments enclosed in the parentheses, and separated by comma Example: f=fact(n); If a call does not require any arguments, an empty pair of parentheses must follow the name. A call may appear itself or it may be one of the operand within a expression. Example: printf( %d,fact(n)); Arguments in call, and formal parameters in declaration must be same in number, type and order. Difference between actual and formal parameters Variables used in calling are called actual parameters or arguments Variables used in definition are called formal parameters or dummy parameters or place holders. Parameter passing mechanisms There are two parameter passing mechanisms 1. call by value. Or passing by value 2. call by reference or passing by address or call by address. Call by value: Passing arguments by value means, the contents of the arguments in the calling are not changed, even if they are changed in the called. This is because the content of the variable is copied to the formal parameter of the definition, thus preventing the contents of the argument in the calling. Call by reference: Call by reference means sending the address of variables as arguments to the. When addresses are sent, the changes occurred in the called can also effect in the calling. /* example to illustrate call by value and call by reference*/ void makezero(int,int*); /* prototype*/ int x=50,y=100; makezero(x,&y); /* call by value and referene */ printf( x=%d y=%d, x,y); /* makezero */ void makezero(int a,int *b) a=0; *b=0; OUTPUT x=50 y=0 Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 28
3 Factorial of number by using long int fact(int); int n; long int f; printf("enter a number\n"); scanf("%d",&n); f = fact(n); printf("the factorial of %d is %ld",n,f); /*end of main */ /* fact*/ long int fact(int x) int i; long int f1=1; for(i=1;i<=x;i++) f1 = f1*i; return(f1); Fibonacci series by int n; void fib(int); printf( "\nhow many Fibonacci numbers required? "); scanf("%d", &n); printf("\nthe first %3d Fibonacci numbers are\n", n); fib(n); void fib(int n) int i, f1, f2, f3; f1 = f2 = 1; EXAMPLE PROGRAMS ON FUNCTIONS printf("%5d\n", f1); printf("%5d\n", f2); for( i = 3; i <= n; i++) f3 = f1 + f2; printf("%5d\n", f3); f1 = f2; f2 = f3; GCD of two numbers by int m,n,g; int gcd(int,int); printf("enter two numbers\n"); scanf("%d%d",&m,&n); g = gcd(m,n); printf("gcd of %d and %d is %d", m,n,g); /* End of main() */ int gcd(int m,int n) int r,g1; do r=m%n; if(r!=0) m=n; n=r; while(r!=0); */ g1=n; return(g1); /* End of gcd Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 29
4 RECURSIVE FUNCTIONS A which calls itself until a certain condition is reached is called recursive /* to find factorial of a number recursively */ int rfactorial(int); int n,f; printf("enter a number\n"); scanf("%d",&n); f=rfactorial(n); printf("factorial of %d is %d",n,f); /* factorial */ int rfactorial(int n) if(n==0) return 1; else return(n*rfactorial(n-1)); /* to find nth fib number recursively */ int rfib(int); int n,f; printf("enter a number\n"); scanf("%d",&n); f=rfib(n); printf("n th fibonacci number is %d",f); /* rfib */ int rfib(int n) if(n==1 n==2) return 1; else return(rfib(n-1)+rfib(n-2)); /* to find GCD of two numbers recursively */ int rgcd(int,int); int m,n,g; printf("enter two numbers\n"); scanf("%d%d",&m,&n); g=rgcd(m,n); printf("gcd is %d",g); /* rgcd */ int rgcd(int m,int n) if(m%n==0) return n; else return(rgcd(n,m%n)); /* to find ncr recursively */ int rncr(int,int); int n,r,s; printf("enter two numbers\n"); scanf("%d%d",&n,&r); s=rncr(n,r); printf("ncr =%d\n",s); int rncr(int n,int r) if(r==0 n==r) return 1; Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 30
5 else return(rncr(n-1,r)+rncr(n-1,r-1)); /* to find sum of digits recursively */ int rsum(int n); int n,s; printf("enter a number\n"); scanf("%d",&n); s=rsum(n); printf("sum of digits =%d\n",s); int rsum(int n) if(n==0) return n; else return(n%10+rsum(n/10)); /* to find the sum of two numbers recursively */ int rsum(int,int); int m,n,s; printf("enter two numbers\n"); scanf("%d%d",&m,&n); s=rsum(m,n); printf("sum =%d\n",s); int rsum(int m,int n) if(n==0) return m; else return(rsum(m+1,n-1)); Difference between iterative and recursive s Iterative 1. Function calls some other s 2. while loop, do while loop or for loop is necessary in iterative 3. Fast in execution since one call is enough to get the result Recursive 1. Function calls some itself 2. if statement is necessary in recursive 3. Slow in execution since several calls are involved to get the result, as number of increases execution will slow down. to value Eg: fact(4) = We require mathematical steps or procedure to write an iterative. 6. Stacks are not used during execution to another call Eg: fact(4) = 4 x fact(3) 5. We require a formula to write a recursive. 6. Stacks are used during execution of recursive s 4. Function call leads 4. Function call leads Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 31
6 STORAGE CLASSES Auto Stati Extern Register Normally the life of a variable is limited to a as long as the is alive. How to make it alive in a file or throughout the program or limiting only to a block inside a or to make common to a desired couple of s etc., The answer lies in STORAGE CLASSES or VARIABLE TYPES. There are four types of storage classes. 1. Automatic variables. 2. External variables. 3. Static variables. 4. Register variables. AUTOMATIC VARIABLES: Automatic variables are declared inside a, in which they are to be utilized. They are created when the is called and destroyed automatically when the is exited. By default all variables are automatic variables. These are also called local or internal variables. EXTERNAL VARIABLES Variables that are both alive and active throughout the entire program are called external variables. These variables are available to all s in that program. Whatever changes that occur in a, will effect the value of that variable. STATIC VARIABLES When a variable is declared as a static variable it is assigned the value zero. Static variables are initialized only once. They will not be initialized for second time during the program. When static is applied to a global variable, the global variable becomes inaccessible outside the file. REGISTER VARIABLES This is local to a or a block. If a compiler finds a physical register in the CPU free for the time being, and also big enough to hold the value, then it may stick that variable in that register. Otherwise the compiler treats that variable as ordinary. It is machine dependent. But compiler will not give error messages even if no register available in reserve. Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 32
7 If we know that a particular variable will be used often inside -say the variable inside a loop-then we can declare that variable with class register. So that the interaction will be very fast //Write a program to demonstrate life cycle of auto variable in nested blocks. auto int x=1; auto int x=2; auto int x=3; printf( %d\n, x); printf( %d\n,x); printf( %d\n,x); Output // program to illustrate automatic and external variables int x=1; int fun1() x=x+10; return x; int fun2() int x; x=20; return x; int fun3() x=1000; return x; int x=100; printf("% d\n",fun1()); printf("%d\n",fun2()); printf("% d\n",fun3()); x=0; printf("%d\n",x); printf("% d\n",fun1()); printf("%d\n",fun2()); printf("% d\n",fun3()); OUTPUT // program to illustrate static variable int fun(); int i; for(i=1;i<5;i++) printf("%d\n",fun()); int fun() static int x=0; x+=1; return(x); OUTPUT // program to illustrate register variable Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 32
8 Stroge Class Type Default initial value auto local Garbage value register int i; int i; for(i=1;i<1000;i++) printf("% d",i); Place of declaration Within or block Scope Only within the or block where it is declared life Until block is no longer active static local zero Until program ends extern global zero Head of all s within file All files including other files where declared extern While any file is active. terminates 5. It is lost when program terminates but not when terminates automatic variable static variable 1. It is declared by auto int a = 10; 1. It is declared by static int a = 10; 2. It must be declared inside the 2. It can be declared inside or outside the 3. If it is not initialized, garbage value is stored 4. It is initialized every time is called that is repeated initialization 5. It is lost when 3. If it is not initialized, zero is stored 4. It is initialized once and only one but not every time when is called Local variable 1. It is declaration inside the 2.It is declared by auto int a=10 or int a = 10 (auto is default scope) 3. If it is not initialized garbage value is Global variable 1.It is declaration outside the 2. It is declared by int a=10 3. If it is not initialized zero is stored Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 33
9 stored Eg: int a; a is garbage value 4. It is created when the starts execution and lost when the terminates 5. It is visible in only one. 6. It can be accessed in only one that is the where it is declared 7. Data sharing is not possible that is data of local variable can be accessed by only one 8. Parameters passing is required for local variable that is local variable of one variable 9. If value of local variable is modified in one changes are not visible in another s Eg: int a; a = 0 4. It is created before program execution starts and lost when program terminates 5. It is visible through out the program. 6. It can be accessed in more than one. 7. Data sharing is possible that is multiple s can access the same global variable. 8. Parameters passing is not required for global variable since global variable is visible throughout the program 9. If value of global variable is modified in one changes are visible in rest of the program Notes Prepared by T. Ratna Reddy. Associate Professor, CBIT Mobile Page 33
Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.
Unit III Functions Functions: Function Definition, Function prototype, types of User Defined Functions, Function calling mechanisms, Built-in string handling and character handling functions, recursion,
More informationUNIT 3 FUNCTIONS AND ARRAYS
UNIT 3 FUNCTIONS AND ARRAYS Functions Function definitions and Prototypes Calling Functions Accessing functions Passing arguments to a function - Storage Classes Scope rules Arrays Defining an array processing
More informationFUNCTIONS OMPAL SINGH
FUNCTIONS 1 INTRODUCTION C enables its programmers to break up a program into segments commonly known as functions, each of which can be written more or less independently of the others. Every function
More information15 FUNCTIONS IN C 15.1 INTRODUCTION
15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.
More informationChapter 4 Functions By C.K. Liang
1 Chapter 4 Functions By C.K. Liang What you should learn? 2 To construct programs modularly from small pieces called functions Math functions in C standard library Create new functions Pass information
More informationUnit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function in C.
1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific task. The functions which are created by programmer are
More information& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.
Computer Programming and Utilization (CPU) 110003 G) Functions 1 What is user defined function? Explain with example. Define the syntax of function in C. A function is a block of code that performs a specific
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Class Teacher: Pralay Mitra Department of Computer Science and Engineering Indian Institute of Technology Kharagpur An Example: Random
More informationFunctions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur
Functions CS10001: Programming & Data Structures Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur 1 Recursion A process by which a function calls itself
More informationC Programming Lecture V
C Programming Lecture V Instructor Özgür ZEYDAN http://cevre.beun.edu.tr/ Modular Programming A function in C is a small sub-program that performs a particular task, and supports the concept of modular
More informationLecture 10: Recursive Functions. Computer System and programming in C 1
Lecture 10: Recursive Functions Computer System and programming in C 1 Outline Introducing Recursive Functions Format of recursive Functions Tracing Recursive Functions Examples Tracing using Recursive
More informationFaculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions
Faculty of Engineering Computer Engineering Department Islamic University of Gaza 2013 C++ Programming Language Lab # 6 Functions C++ Programming Language Lab # 6 Functions Objective: To be familiar with
More informationFunctions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR
1 Functions Functions are everywhere in C Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Introduction Function A self-contained program segment that carries
More informationDepartment of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06
Department of Computer Science & Engineering Indian Institute of Technology Kharagpur Practice Sheet #06 Topic: Recursion in C 1. What string does the following program print? #include #include
More informationUnit 7. Functions. Need of User Defined Functions
Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have
More informationProgramming & Data Structure Laboratory. Day 2, July 24, 2014
Programming & Data Structure Laboratory Day 2, July 24, 2014 Loops Pre and post test loops for while do-while switch-case Pre-test loop and post-test loop Condition checking True Loop Body False Loop Body
More informationDarshan Institute of Engineering & Technology for Diploma Studies Unit 3
Linear and Non-Linear Data Structures Linear data structure: Linear data structures are those data structure in which data items are arranged in a linear sequence by physically or logically or both the
More informationA PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions.
FUNCTIONS IN C A PROBLEM can be solved easily if it is decomposed into parts. Similarly a C program decomposes a program into its component functions. Big problems require big programs too big to be written
More informationUNIT III (PART-II) & UNIT IV(PART-I)
UNIT III (PART-II) & UNIT IV(PART-I) Function: it is defined as self contained block of code to perform a task. Functions can be categorized to system-defined functions and user-defined functions. System
More informationFunctions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.
Lab 4 Functions Introduction: A function : is a collection of statements that are grouped together to perform an operation. The following is its format: type name ( parameter1, parameter2,...) { statements
More informationTwo Approaches to Algorithms An Example (1) Iteration (2) Recursion
2. Recursion Algorithm Two Approaches to Algorithms (1) Iteration It exploits while-loop, for-loop, repeat-until etc. Classical, conventional, and general approach (2) Recursion Self-function call It exploits
More informationProgramming & Data Structure Laboratory. Arrays, pointers and recursion Day 5, August 5, 2014
Programming & Data Structure Laboratory rrays, pointers and recursion Day 5, ugust 5, 2014 Pointers and Multidimensional rray Function and Recursion Counting function calls in Fibonacci #include
More informationProgramming & Data Structure
Functions Programming & Data Structure CS 11002 Partha Bhowmick http://cse.iitkgp.ac.in/ pb CSE Department IIT Kharagpur Spring 2012-2013 Functions Callee : Caller = Bowler : Captain Functions int f(){...
More informationStorage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.
1 Storage class in C Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables. 2 Few terms 1. Scope: the scope of a variable determines
More informationSUHAIL T A
SUHAIL T A Functions A number of statements grouped into a single logical unit C pgm is a collection of functions Self contained pgm segment that carries out a specific task main is a special recognized
More informationPESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities
PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities Continuous Internal Evaluation Test 2 Date: 0-10- 2017 Marks: 0 Subject &
More informationCS 101: Computer Programming and Utilization
CS 101: Computer Programming and Utilization Jul - Nov 2016 Bernard Menezes (cs101@cse.iitb.ac.in) Lecture 13: Recursive Functions About These Slides Based on Chapter 10 of the book An Introduction to
More informationFunctions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa
Functions Autumn Semester 2009 Programming and Data Structure 1 Courtsey: University of Pittsburgh-CSD-Khalifa Introduction Function A self-contained program segment that carries out some specific, well-defined
More informationUnit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION
INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of
More informationFunction. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers
Functions 1 Function n A program segment that carries out some specific, well-defined task n Example A function to add two numbers A function to find the largest of n numbers n A function will carry out
More informationCSE 230 Intermediate Programming in C and C++ Functions
CSE 230 Intermediate Programming in C and C++ Functions Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse230/ Concept of Functions
More informationFunctions Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay
Functions 60-141 Introduction to Algorithms and Programming II School of Computer Science Term: Summer 2013 Instructor: Dr. Asish Mukhopadhyay Motivation A complex program Approximate Ellipse Demo Ellipse2DDouble.java
More informationIntroduction to Programming
Introduction to Programming Lecture 8: Functions What We Will Learn Introduction Passing input parameters Producing output Scope of variables Storage Class of variables Function usage example Recursion
More informationSums. Here's a (poor) program to add three numbers
Arrays 1 2 Sums 3 Here's a (poor) program to add three numbers /* Add three numbers */ #include add.c int main() { int numbers[3]; numbers[0] = 16; numbers[1] = 12; numbers[2] = 14; int sum =
More informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationFunctions and Recursion
Functions and Recursion CSE 130: Introduction to Programming in C Stony Brook University Software Reuse Laziness is a virtue among programmers Often, a given task must be performed multiple times Instead
More information1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code
1. Basics 1. Write a program to add any two-given integer. Algorithm - 1. Start 2. Prompt user for two integer values 3. Accept the two values a & b 4. Calculate c = a + b 5. Display c 6. Stop int a, b,
More informationApplied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011
Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011 Autumn 2015 Lecture 3, Simple C programming M. Eriksson (with contributions from A. Maki and
More informationCSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions
CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:
More informationData Structures And Algorithms
Data Structures And Algorithms Recursion Eng. Anis Nazer First Semester 2016-2017 Recursion Recursion: to define something in terms of itself Example: factorial n!={ 1 n=0 n (n 1)! n>0 Recursion Example:
More informationFunctions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.
Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create
More informationC Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 2 (Minor modifications by the instructor) 1 Scope Rules A variable declared inside a function is a local variable Each local variable in a function comes into existence when the function
More informationFundamentals of Programming & Procedural Programming
Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Four: Functions: Built-in, Parameters and Arguments, Fruitful and Void Functions
More informationTest Paper 3 Programming Language Solution Question 1: Briefly describe the structure of a C program. A C program consists of (i) functions and (ii) statements There should be at least one function called
More informationFunctions. (transfer of parameters, returned values, recursion, function pointers).
Functions (transfer of parameters, returned values, recursion, function pointers). A function is a named, independent section of C/C++ code that performs a specific task and optionally returns a value
More informationECE 2574: Data Structures and Algorithms - Recursion Part I. C. L. Wyatt
ECE 2574: Data Structures and Algorithms - Recursion Part I C. L. Wyatt Today we will introduce the notion of recursion, look at some examples, and see how to implement them in code. Introduction to recursion
More informationStandard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing
Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion Copyright 2003 Scott/Jones Publishing Topics 19.1 Introduction to Recursion 19.2 The Recursive Factorial Function 19.3 The Recursive
More informationLAB 7 FUNCTION PART 2
LAB 7 FUNCTION PART 2 School of Computer and Communication Engineering Universiti Malaysia Perlis 1 OBJECTIVES 1. To differentiate the file scope and block scope. 2. To write recursive function. 3. To
More informationComputer Programming Unit 3
POINTERS INTRODUCTION Pointers are important in c-language. Some tasks are performed more easily with pointers such as dynamic memory allocation, cannot be performed without using pointers. So it s very
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 6 Modular programming Department of Computer Engineering Outline Introduction to pointer
More informationStructured Programming. Functions and Structured Programming. Functions. Variables
Structured Programming Functions and Structured Programming Structured programming is a problem-solving strategy and a programming methodology. The construction of a program should embody topdown design
More informationC Functions. Object created and destroyed within its block auto: default for local variables
1 5 C Functions 5.12 Storage Classes 2 Automatic storage Object created and destroyed within its block auto: default for local variables auto double x, y; Static storage Variables exist for entire program
More information공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE
공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE LOOPS WHILE AND FOR while syntax while (expression) statement The expression is evaluated. If it is
More informationProgramming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3
Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components
More informationWhat Is a Function? Illustration of Program Flow
What Is a Function? A function is, a subprogram that can act on data and return a value Each function has its own name, and when that name is encountered, the execution of the program branches to the body
More informationModule 6: Array in C
1 Table of Content 1. Introduction 2. Basics of array 3. Types of Array 4. Declaring Arrays 5. Initializing an array 6. Processing an array 7. Summary Learning objectives 1. To understand the concept of
More informationGovernment Polytechnic Muzaffarpur.
Government Polytechnic Muzaffarpur. Name of the Lab: COMPUTER PROGRAMMING LAB (MECH. ENGG. GROUP) Subject Code: 1625408 Experiment: 1 Aim: Programming exercise on executing a C program. If you are looking
More informationFunctions. P. S. Suryateja startertutorials.com [short domain - stuts.me] 2
UNIT - 3 FUNCTIONS Functions Until now, in all the C programs that we have written, the program consists of a main function and inside that we are writing the logic of the program. The disadvantage of
More informationRecursion. CSE 2320 Algorithms and Data Structures University of Texas at Arlington
Recursion CSE 2320 Algorithms and Data Structures University of Texas at Arlington Updated: 2/21/2018 1 Background & Preclass Preparation Background (review): Recursive functions Factorial must know how
More informationUNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru.
UNIT 2: STACK & RECURSION Programs demonstrated in class. Tojo Mathew Asst. Professor CSE Dept., NIE Mysuru. Table of Contents 1. C Program to Check for balanced parenthesis by using Stacks...3 2. Program
More informationChapter 7 Functions. Now consider a more advanced example:
Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done
More informationM1-R4: Programing and Problem Solving using C (JAN 2019)
M1-R4: Programing and Problem Solving using C (JAN 2019) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter
More information'C' Programming Language
F.Y. Diploma : Sem. II [DE/EJ/ET/EN/EX] 'C' Programming Language Time: 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1(a) Define pointer. Write syntax
More informationRecursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?
Recursion Chapter 8 CS 3358 Summer I 2012 Jill Seaman What is recursion? Generally, when something contains a reference to itself Math: defining a function in terms of itself Computer science: when a function
More information***************OUTPUT*************************** Enter the two number The addition of two number is =100
/* 1. Program to calculate the addition of two number using function */ int n1,n2,result; printf(" Enter the two number \n"); scanf("%d%d",&n1,&n2); result=addnum(n1,n2); printf(" The addition of two number
More informationComputer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];
Arrays An array is a collection of two or more adjacent memory cells, called array elements. Array is derived data type that is used to represent collection of data items. C Array is a collection of data
More informationFunctions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction
Functions Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur Programming and Data Structure 1 Function Introduction A self-contained program segment that
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays 1 What are Arrays? Arrays are our first example of structured data. Think of a book with pages numbered 1,2,...,400.
More informationAN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University
AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:
More informationCS113: Lecture 4. Topics: Functions. Function Activation Records
CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make
More informationPROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE
PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE 1. Write a C program to perform addition, subtraction, multiplication and division of two numbers. # include # include int a, b,sum,
More informationEECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution
1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can
More informationSUMMER 13 EXAMINATION Model Answer
Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme. 2) The model answer and the answer written by candidate
More informationChapter-13 USER DEFINED FUNCTIONS
Chapter-13 USER DEFINED FUNCTIONS Definition: User-defined function is a function defined by the user to solve his/her problem. Such a function can be called (or invoked) from anywhere and any number of
More informationOVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?
CH. 5 RECURSION ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER (WILEY 2016) OVERVIEW Recursion is an algorithmic
More informationScope, Functions, and Storage Management
Scope, Functions, and Storage Management Implementing Functions and Blocks cs3723 1 Simplified Machine Model (Compare To List Abstract Machine) Registers Code Data Program Counter (current instruction)
More informationWhat is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.
Recursion What is recursion? Week 10 Generally, when something contains a reference to itself Gaddis:19.1-19.5 CS 5301 Spring 2014 Jill Seaman 1 Math: defining a function in terms of itself Computer science:
More informationChapter 9. Variable Scope and Functions
Chapter 9. Variable Scope and Functions So far, we have been using only global variables. In this chapter, we will learn about other kinds of variables and how to use them. This chapter also tells you
More informationME 172. C Programming Language Sessional Lecture 8
ME 172 C Programming Language Sessional Lecture 8 Functions Functions are passages of code that have been given a name. C functions can be classified into two categories Library functions User-defined
More informationF.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C
F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C Time : 3 Hrs.] Prelim Question Paper Solution [Marks : 70 Q.1 Attempt any FIVE of the following : [10] Q.1 (a) List any four relational operators.
More information8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );
- 50 - Control Structures: 8. Functions (II) Arguments passed by value and by reference. Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This
More informationRecursive Definitions
Recursion Objectives Explain the underlying concepts of recursion Examine recursive methods and unravel their processing steps Explain when recursion should and should not be used Demonstrate the use of
More informationRecursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.
Recursion Chapter 8 CS 3358 Summer II 2013 Jill Seaman What is recursion?! Generally, when something contains a reference to itself! Math: defining a function in terms of itself! Computer science: when
More informationProcedural programming with C
Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical
More informationComputer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers
Computer Programming: 7th Week Functions, Recursive Functions, Introduction to Pointers Hazırlayan Asst. Prof. Dr. Tansu Filik Introduction to Programming Languages Previously on Bil-200 Functions: Function
More informationCHAPTER 4 FUNCTIONS. 4.1 Introduction
CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main
More informationRecursive definition: A definition that is defined in terms of itself. Recursive method: a method that calls itself (directly or indirectly).
Recursion We teach recursion as the first topic, instead of new object-oriented ideas, so that those who are new to Java can have a chance to catch up on the object-oriented ideas from CS100. Recursive
More informationAnatomy of a static method, S&W, 2.1, figure page 188. Class.name or just name Scope, S&W, 2.1, figure page 189. Overloading
Static Methods Anatomy of a static method, S&W, 2.1, figure page 188. Class.name or just name Scope, S&W, 2.1, figure page 189. Overloading public static int abs (final int x) { return x
More informationCS 310 Advanced Data Structures and Algorithms
CS 310 Advanced Data Structures and Algorithms Recursion June 27, 2017 Tong Wang UMass Boston CS 310 June 27, 2017 1 / 20 Recursion Recursion means defining something, such as a function, in terms of itself
More informationVTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS
Subject: Data Structure with C Topic: Recursion In this chapter we are departing recursion concepts through the following steps as millstones. Presentation starts with recursion definition, how recursion
More informationChapter 5 C Functions
Chapter 5 C Functions Objectives of this chapter: To construct programs from small pieces called functions. Common math functions in math.h the C Standard Library. sin( ), cos( ), tan( ), atan( ), sqrt(
More informationFunctions. Chapter 5
Functions Chapter 5 Function Definition type function_name ( parameter list ) declarations statements For example int factorial(int n) int i, product = 1; for (i = 2; I
More informationFunctions BCA-105. Few Facts About Functions:
Functions When programs become too large and complex and as a result the task of debugging, testing, and maintaining becomes difficult then C provides a most striking feature known as user defined function
More informationLECTURE 06 FUNCTIONS
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 06 FUNCTIONS IMRAN
More informationRecursion. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function A from within function A!
Recursion Definition: Any time the body of a function contains a call to the function itself. So, just as you are allowed to call function B from within function A, you are ALSO allowed to call function
More informationChapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.
Chapter 4: Expressions Chapter 4 Expressions 1 Chapter 4: Expressions Operators Expressions are built from variables, constants, and operators. C has a rich collection of operators, including arithmetic
More informationBBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$617$
BBM#101# #Introduc/on#to# Programming#I# Fall$2013,$Lecture$617$ Instructors:AykutErdem,ErkutErdem,FuatAkal TAs:YasinSahin,AhmetSelmanBozkir,GultekinIsik, OguzhanGuclu Today# Func/ons# Defini@ons Invoca@on
More informationIMPORTANT QUESTIONS IN C FOR THE INTERVIEW
IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.
More informationBBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7
BBM 101 Introduc/on to Programming I Fall 2013, Lecture 6-7 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Yasin Sahin, Ahmet Selman Bozkir, Gultekin Isik, Oguzhan Guclu 1 Today Func/ons Defini@ons
More information11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1
RECURSION Chapter 5 Recursive Thinking Section 5.1 1 Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that are difficult
More information