SUHAIL T A

Similar documents
Unit 3 Functions. 1 What is user defined function? Explain with example. Define the syntax of function 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.

& Technology. G) Functions. void. Argument2, Example: (Argument1, Syllabus for 1. 1 What. has a unique. 2) Function name. passed to.

Unit 7. Functions. Need of User Defined Functions

CSE202-Lec#4. CSE202 C++ Programming

UNIT 3 FUNCTIONS AND ARRAYS

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Subject: Fundamental of Computer Programming 2068

C Programming Lecture V

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Function a block of statements grouped together, to perform some specific task

Functions. Lecture 6 COP 3014 Spring February 11, 2018

15 FUNCTIONS IN C 15.1 INTRODUCTION

FUNCTIONS OMPAL SINGH

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Functions. printf( %f cubed is %f,x,cube(x)); return 0; } return parameter type double cube(double var) { return (var*var*var);

Functions. Arash Rafiey. September 26, 2017

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Function. 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

Lecture 9 - C Functions

CSE 230 Intermediate Programming in C and C++ Functions

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Chapter 5 C Functions

Lecture 5: Methods CS2301

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Chapter-13 USER DEFINED FUNCTIONS


UNIT III (PART-II) & UNIT IV(PART-I)

Fundamentals of Programming & Procedural Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

ME 172. C Programming Language Sessional Lecture 8

Chapter 8: Function. In this chapter, you will learn about

Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Chapter 4 Functions By C.K. Liang

M1-R4: Programing and Problem Solving using C (JAN 2019)

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

DECISION CONTROL AND LOOPING STATEMENTS

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Introduction to Programming (Java) 4/12

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

FUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value

Government Polytechnic Muzaffarpur.

C Functions. 5.2 Program Modules in C

Prepared by: Shraddha Modi

OBJECTIVE QUESTIONS: Choose the correct alternative:

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Chapter 7 Functions. Now consider a more advanced example:

CSCI 2132 Software Development. Lecture 18: Functions

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

LECTURE 3. Compiler Phases

Conditional Statement

COMP 208 Computers in Engineering

Multiple Choice Questions ( 1 mark)

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

CS113: Lecture 4. Topics: Functions. Function Activation Records

Functions BCA-105. Few Facts About Functions:

AMCAT Automata Coding Sample Questions And Answers

Pointer Data Type and Pointer Variables

Lecture 8 Tao Wang 1

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

LAB 6 FUNCTION PART 1 School of Computer and Communication Engineering Universiti Malaysia Perlis

Functions. (transfer of parameters, returned values, recursion, function pointers).

IV Unit Second Part STRUCTURES

Computer Programming

Scope. Scope. Region of a program in which a defined object is visible. Defined Objects. Two types of regions. Variables Functions

Lecture 2: C Programming Basic

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

Problem Solving and 'C' Programming

C Functions. Object created and destroyed within its block auto: default for local variables

UNDERSTANDING THE COMPUTER S MEMORY

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

CS1150 Principles of Computer Science Methods

EC 413 Computer Organization

BSM540 Basics of C Language

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Functions. Systems Programming Concepts

Questions Bank. 14) State any four advantages of using flow-chart

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Unit 3 Decision making, Looping and Arrays

Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Padasalai.Net s Model Question Paper

Technical Questions. Q 1) What are the key features in C programming language?

AROUND THE WORLD OF C


Functions in C C Programming and Software Tools

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

An array is a collection of similar elements that is all the elements in an array should have same data type. This means that an array can store

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Methods. CSE 114, Computer Science 1 Stony Brook University

Transcription:

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 function

Functions-Advantages Facilitates top-down modular pgming- high level logic is solved first while the details of lower level fns are addressed later Avoids complexity of a pgm-avoiding rewriting the same code over and over Each fn can be individually tested, so debugging and testing are easy Can be used by many other pgms, so save time and reduce the length of the pgm Pgms with fns are easy to maintain because modifications can be done to certain fns within the pgm

Functions- modular pgmming Main Pgm Function A Function B Function C B1 B2

Functions Two Types 1. Library Functions 2. User defined functions 1. Library Functions Built in functions, used to perform well defined tasks Eg. printf(), scanf(),gets(),puts(), gechar(),putchar() Can be either input or output task or some mathematical and scientific tasks 2. User defined functions Written by the programmer according to his need

Concepts associated with Functions 1. Function declaration/function prototype A function declaration provides the following information to the compiler 1. The name of the fn 2. The type of the value returned 3. The number and the type of arguments that must be supplied in a call to the fn

Concepts associated with Functions Syntax return-data-type function-name(type,type,type.type); Eg. int cube(int); The fn cube has one argument of type int. It returns an int value

Concepts associated with Functions 2. Function Definition Similar to fn declaration but does not have semicolon Consists of 3 main components a. Fn header b. Declaration of arguments c. Body of the fn

Concepts associated with Functions Syntax return-data-type function name(formal argument 1, formal argumantn) declaration of variables; statements; return (exp); Eg. double cube(double num) return num*num*num; Body of the fn

Concepts associated with Functions 3. Elimination of fn declaration If the fns are defined before they are called, then the declarations are necessary Eg. #include<stdio.h> int max(int a,int b) return a>b?a:b; void main(void) int i,j,imax; printf( Enter two numbers ); scanf( %d%d,&i,&j); imax=max(i,j); printf( the max of %d & %d is %d,i,j,imax);

Concepts associated with Functions 4. return statement Used to return any value from the fn called Terminates the fn and returns a value to its caller Also used to exit from any fn without any returning any value Syntax return; return(exp);

Concepts associated with Functions Eg. #include<stdio.h> int cube(int); void main() int i; for(i=0;i<=10;i++) printf( %d,cube(i)); int cube(int y) return y*y*y;

Concepts associated with Functions 5. Function Call A fn Comes to life when a call is made to the fn Specified by the fn name followed by the values of parameters enclosed within the parenthesis, terminated by semi colon. Rules for fn call 1. Number of actual and formal parameters are same 2. Type of actual and formal parameters are same 3. Sequence of actual and formal parameters are same

Concepts associated with Functions Eg. #include<stdio.h> void main() void text(void); void text(void) printf( welcome );

Function Parameters/ Actual and formal Function Parameters Arguments Means of commnbtnthe calling and the called fn Classified into Actual Parameters and formal Parameters Formal Parameters Given in the fn declaration and fn definition Dummy Parameters Actual Parameters Parameters/variables defined in the main pgm

Function Parameters/ Actual and formal Eg. #include<stdio.h> int sum(int,int); void main() int a,b; Arguments printf( Enter two numbers ); scanf( %d%d,&a,&b); printf( %d,sum(a,b)); int sum(int x,int y) return x+y;

Categories of Functions 1. Function with no arguments and no return values or empty parameter list An empty parameter list refers to the list with no arguments or parameters A fn can be defined with empty parameter list Specified by writing either voidor nothing at all in the parenthesis of fn definition

Categories of Functions Eg. #include<stdio.h> void text1(); void text2(); void main() text1(); text2(); void text1(); printf( Function text1 takes no arguments ); void text2() printf( Function text2 also takes no arguments );

2. Function with arguments and no return values Eg. #include<stdio.h> void largest(int,int); void main() int a,b; printf( Enter two numbers ); scanf( %d%d,&a,&b); largest(a,b); getch(); void largest(int a,int b) if (a>b) printf( largest element is %d,a); else printf( largest element is %d,b);

Categories of Functions 3. Function with arguments and return values Eg. #include<stdio.h> int add(int,int,int); void main() int a,b,c,sum; printf( Enter three numbers ); scanf( %d%d%d,&a,&b,&c); sum=add(a,b,c); printf( sum=%d,sum); int add (int x,int y,int z) int s; s=x+y+z; return(s);

A fn calls itself Eg. main() Recursion printf( this is an example of recursion\n ); main(); o/p- execution will continue indefinitely this is an example of recursion this is an example of recursion this is an example of recursion

Recursion Evaluation of factorials of a given no Factorial of n=n*(n-1)*(n-2).*1 Factorial of 4=4*3*2*1 factorial (int n) int fact; if (n==1) return(1); else fact=n*factorial(n-1); return(fact);

Here fact=n*factorial(n-1) Assume n=3 fact=3*factorial(2) =3*2*factorial(1) =3*2*1 =6 Recursion

Passing Arguments An argument can be passed from main() to the fn in two ways 1. Call by value 2. Call by reference 1. Call by value Refers to sending value of arguments into the fn Value of actual arguments is copied onto the corresponding formal arguments whenever a fn is called Operations performed on the formal arguments in the fn has no effect on the value of actual arguments in the main fn

main() int i=10; print(); Passing Arguments print (int i) printf( %d,i); return;

2. Call by Reference Passing Arguments Method to access any fn through the main pgm using pointers Operations performed on formal arguments directly effect the value of actual arguments Formal contains the address of actual arguments, so any changes on that address effect the value

Keep in touch through suhailta@gmail.com