Functions in C Computer Programming(1)- 1090CS Manesh T

Slides:



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

User Defined Functions
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
Chapter Five Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
1 ICS103 Programming in C Lecture 7: Introduction to 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.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
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.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
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: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Arrays 1090CS, Computer Programming 1 Manesh T
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition 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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Functions The fruitful & parameterized functions.
Algorithm & Flow Charts Decision Making and Looping
User-Written Functions
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Functions, Part 2 of 2 Topics Functions That Return a Value
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Tejalal Choudhary “C Programming from Scratch” Function & its types
User Defined Functions
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
In C Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

Functions in C Computer Programming(1)- 1090CS Manesh T

2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions Different types of functions Flow of Execution

Define Functions 3 A function is program segment which performs a well defined task Big programs are divided into small programs

4 Advantages of Using Functions 1.Reduce size of main program 2.Easy to understand 3.Easy error handling

5 Standard (Predefined) Functions Predefined functions Part of the C language Provided in function libraries abs(x), sin(x), log(x), pow( x, n) Examples: abs(x), sin(x), log(x), pow( x, n) These functions will return a value y = pow (3, 4.5); To be assigned y = pow (3, 4.5); 3.14 * sqr(r) To be used in an expression 3.14 * sqr(r) #include Make sure to use the required #include file

6 Predefined Functions

Model 7 #include void main ( )printf is the name of a predefined {function in the stdio library printf (“Hello World!\n”) ; this statement is is known as a } function call this is a string we are passing as an argument (parameter) to the printf function

Elements of a function Function Definition Function Call Function Declaration 8

Function Definition Function Type, Name, list of parameters Local variable declaration Function statements Return statements 9

Format of the function definition Data type fun_name( type1 arg1,……typen arg n) { Local variables declarations function statement1; function statement2; return Statement; } 10 Function Type Name of function List of parameters (formal arguments) Return statement

Example int sum(int a, int b) { int sum=0; sum=a+b; return (sum); } 11 Function Type Name of function List of parameters (formal arguments) Return statement

Function Call-format Name_function(arg1, arg2,……..,arg n) Eg: int c; c=sum(a, b); 12 List of parameters (Actual Parameters)

Function Declaration Type Name_fun(type 1 arg1,… typen argn); Eg: int sum(int a, int b); 13

14 Examining printMessage #include void printMessage ( ) ; function declaration void main ( ) { printMessage ( ) ; function call } void printMessage ( )function header { printf (“A message for you:\n\n”) ; function printf (“Have a nice day!\n”) ; body } function definition

Model Programs Write a program to add two numbers using functions Write a program to find average of three numbers using functions Write a program to find factorial of number using functions Practice: Develop by yourown Write a program to find even or odd using function

Write a program to add two numbers using functions #include int sum(int x,int y) { int z; z=x+y; return(z); } void main() { int a,b,c; printf("\nEnter two numbers: "); scanf("%d%d",&a,&b); c=sum(a,b); printf("\nSum=%d",c); } 16

Write a program to find average of three numbers using functions #include float average(int x,int y,int z) { float av; av=(float)(x+y+z)/3; return(av); } void main() { int a,b,c; float avg; printf("\nEnter three numbers: "); scanf("%d%d%d",&a,&b,&c); avg=average(a,b,c); printf("\nAverage=%f",avg); } 17

Write a program to find factorial of number using functions #include int factorial(int n) { int i,f=1; for(i=1;i<=n;i++) { f=f*i; } return(f); } void main() { int i,n,fact=1; printf("\nEnter n:"); scanf("%d",&n); fact=factorial(n); printf("\nFactorial=%d\n",fact); }

#include void main() { } int n,f; printf("Enter n:"); scanf("%d",&n); f=factorial(n); printf("\nFactorial=%d\n",f); int factorial(int n) { int i,fact=1; for(i=1;i<=n;i++) { fact=fact*i; } return(fact); } Function n fact

Model Questions from Functions Define Functions? See Slide No 3 What are advantages of functions? See Slide No 4 Explain function definition with example? See Slide No 10 & 11 Explain Function call with example? See Slide No 12 20