5 Functions Ppt

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 5 Functions Ppt as PDF for free.

More details

  • Words: 742
  • Pages: 16
Day 5.

Functions.

What is a Function ? • Function is a self contained block of statements that perform a coherent task of some kind. • Every C program can be a thought of the collection of functions. • main( ) is also a function.

Types of 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 . • User defined functions. Programmer can create their own function in C to perform specific task.

User defined functions. • e.g. #include<stdio.h> main( ) { message( ); } message( ) { printf(“Hello”); }

Declaration of many functions. #include<stdio.h> main( ) { printf(“I am in main”); poly( ); engg( ); agri( ); } poly( ) { printf(“I am in polytechnic”); } engg( ) { printf(“I am in engineering”); } agri( ) { printf(“I am in agri”); }

Some conclusions. • Any C Program must contain at least one function. • If program contains only one function it must be main( ). • There is no limit on the number of functions present in a C program. • Each function in a program is called in the sequence specified by functions call in main( ). • After each function has done its thing, control returns to main( ). When main( ) run out of function calls program ends.

Summarization. • •

C Program is a collection of one or more functions. A function gets called when its name is followed by a semicolon. e.g. main( ) { fun( ); } fun( ) { statement1; statement2; statement3; }

Summarization Contd………. • Any function can be called by any other function. e.g. main( ) { printf(“I am in main.”); fun( ); } fun( ) { printf(“I am in fun.”); main( ); } This will run in infinity.

Summarization Contd………. • A function can be called by number of times. e.g. main( ) { printf(“I am in main.”); fun( ); fun( ); fun( ); } fun( ) { printf(“I am in fun.”); }

Summarization Contd………. • A function can call itself, it is called as a ‘recursion’. e.g. main ( ) { printf(“I am in main”); main( ); } • A function can be called from another function but can not be defined in another function. It should be defined outside of the main.

Why use functions ? • Writing functions avoids rewriting of the same code again and again in the program. • Using a function it becomes easier to write program to keep track of what they are doing.

Passing values to functions. The values that we pass to the function called as function arguments. e.g. main( ) { int i=10, j=12, k=20; calsum(i,j,k); } calsum(int x, int y, int z) { printf(“Sum is : %d”,(x+y+z)); } i, j, k are actual arguments x, y, z are formal arguments.

Returning a value. • The keyword ‘return’ to used to return value from function which is present in the parenthesis of return. • On executing return statement it immediately transfers control back to the main program. e.g. main( ) { int a = 30, j = 14; int k; k = addin(a, j); printf(“%d”, k); } int addin(int x, int y) { int z; z = (x + y) +1; return(z); }

return • There is no restriction of return statements in a function. • Whenever the control returns from the function some value is definitely returned. It should be accepted in the calling program. e.g. int sum; sum = calsum(a,b,c); here the value returned by calsum must be of type int. • Some valid return statements. return a; /*a is a variable name */ return (23); return (15.32); return (‘v’); return; • If you are not returning any value from a function they it should be declared as void. (void is a keyword). e.g. void display( ) display is not returning any value.

Function declaration and Prototype. Any C function by default returns and integer value. More specifically, whenever a call is made to a function, the compiler assumes that this function would return value of type int. Function prototype : #include<stdio.h> int addin(int, int); void main( ) { statements; statements; } int addin(int a, int b) { statements; }

Function prototype.

Created By, • Mr. Tushar B Kute, Lecturer in Information Technology, K. K. Wagh Polytechnic, Nashik. [email protected]

Related Documents

5 Functions Ppt
November 2019 6
5.ppt
April 2020 4
Functions
November 2019 47
Functions
December 2019 49
Functions
May 2020 27