FUNCTIONS.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Spring Semester 2013 Lecture 5
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
CS 201 Functions Debzani Deb.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
User defined functions
FUNCTIONS A function is a set of instructions that can perform a specific task accordingly. A function is a program that performs a specific task according.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
1 ICS103 Programming in C Lecture 8: Functions I.
EC201- FUNCTIONS CONTROL STATEMENT DTK, JKE, PTSB (V 10.12)
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions The fruitful & parameterized functions.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Chapter 7: Function.
EKT120: Computer Programming
User-Written Functions
Chapter 4 Function and Structure.
Chapter 6: User-Defined Functions I
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
FUNCTIONS In C++.
Object Oriented Systems Lecture 03 Method
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
EKT120: Computer Programming
Functions in C Mrs. Chitra M. Gaikwad.
CSCI 161: Introduction to Programming Function
Visit for more Learning Resources
Buy book Online -
User-defined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Tejalal Choudhary “C Programming from Scratch” Function & its types
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
6 Chapter Functions.
User-defined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Lec8.
A function with one argument
Function In this lesson, you will learn about Introduction to Function
C PROGRAMMING LECTURE METHODS
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Computing Lecture 08: Functions (Part I)
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Course Outcomes of Programming In C (PIC) (17212, C203):
CPS125.
Presentation transcript:

FUNCTIONS

What is a Function ? Types of Functions Function is a self- contained block or a sub-program of one or more statements that perform a specific task when called. Types of Functions 1. Library functions. 2. User-defined functions. Library functions These are the in- built functions of ‘C’ library. These are already defined in header files. e.g. printf (); is a function which is used to print ( ); at output. It is defined in ‘stdio.h ’file

Need for User defined functions Programmer can create their own function in C to perform specific task. Need for User defined functions If we want to perform a task repetitively then it is not necessary to re-write the particular block of the program again and again .Shift the particular block of statements in a user-defined function. The function defined can be used any number of times to perform the task. Using functions large program can be reduced to smaller one called sub-programs. It is easy to debug and find out the errors in it.

Elements of User defined functions Function definition Function call Function declaration Definition of function Function definition is also called function implementation. function name; function type; list of parameters; local variable declarations; function statements; a return statements. All the six elements are grouped into two parts: 1. Function header (first three elements) 2. Function body (second three elements)

A general format of a function definition: function _type function _name (parameter list) { local variable declarations; executable statement1; executable statement2; executable statement3; ………………………… …………………………. return statements; }

Function declaration Function declaration is also called function prototype. It consist of 4 types: Function type Function name parameters list Terminating semicolon. general format function _type function _name (parameter list) ;

NOTE The parameter list must be separated by commas. The parameter names do not need to be same in the prototype declaration and the function definition. Use of parameter names in the declaration is optional. If the function has no parameters , the list is written as (void). EX: int mul (int a,int b); void mul (void)

Function call A function can be called by simply using the function name followed by a list of parameters (or arguments) if any in parentheses, terminated by semicolon (;). void printline(void); //function declaration main() { printline(); //function call printf(" c functions\n"); printline(); getch(); } void printline(void) // function definition int i; for(i=1;i<40;i++) printf("-"); printf("\n"); return;

int mul(int a,int b); //function declaration main() { int y; y = mul(10,2); //function call printf("%d\n",y); getch(); } int mul(int x,int y) //function definition int p; p=x*y; //called function return(p);

Types of Functions Functions with no arguments and no return values. Functions with arguments and no return values. Functions with arguments and return values. Functions with no arguments and return values. Functions that return multiple values.

TYPE-1 Functions with no arguments and no return values

#include<stdio.h> #include<conio.h> void printline(void); main() { printf("Welcome to functions in C"); printline(); printf("Function are easy to learn."); getch(); } void printline(void) int i; printf("\n"); for(i=0;i<30;i++) printf("*"); return;

TYPE-2 Functions with arguments and no return values

#include<stdio.h> #include<conio.h> void add(int x, int y) ; main() { add(30,15); add(63,49); add(952,321); getch(); } void add(int x, int y) int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); return;

TYPE-3 Functions with arguments and return values

#include<stdio.h> #include<conio.h> int add(int x, int y); main() { int z; z = add(50,50); printf("Result %d.\n\n",z); printf("Result %d.\n\n",add(30,55)); getch(); } int add(int x, int y) int result; result = x+y; return(result);

TYPE-4 Functions with no arguments and return values

#include<stdio.h> #include<conio.h> int send(void); main() { int z; z = send(); printf("\nYou entered : %d.", z); getch(); } int send(void) int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1);

TYPE-5 Functions that return multiple values #include<stdio.h> #include<conio.h> main() { int a=20, b=20, p,q; calc(a,b,&p,&q); printf("Sum =  %d, Sub = %d",p,q); getch(); } void calc(int x, int y, int *add, int *sub) *add = x+y; *sub = x-y;

Two ways of passing arguments to the function Call by value Call by reference Call by value In this type value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any change made in the formal arguments does not affect the actual arguments. When function is called by the call or by value method, it does not affect the actual content of the actual arguments.

Call by value #include <stdio.h> #include <conio.h> int swap(int,int) ; main() { int a=50, b=70; swap(a,b); printf ("a=%d b=%d", a, b); getch(); } int swap (int a1,int b1) int t; t=a1; a1=b1; b1=t; printf("a1=%d b1=%d\n",a1,b1); return;

Call by reference #include <stdio.h> #include <conio.h> int swap(int *a,int *b); main() { int x,y; printf("enter value of X and Y:"); scanf("%d %d",&x,&y); swap(&x,&y); printf("\n In MAIN X=%d Y=%d",x,y); getch(); } int swap(int *a,int *b) int k; k=*a; *a=*b; *b=k; printf("X=%d Y=%d",*a,*b);