functions in c

Views:
 
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

SARDAR PATEL INSTITUTE OF TECHNOLOGY AND MANAGEMENT: 

SARDAR PATEL INSTITUTE OF TECHNOLOGY AND MANAGEMENT DEPT. OF COMPUTER SCIENCE ENGINEERING Prepared by :-Himanshu Paliwal Guided by :- Ashutosh Yadav Dept. computer Science CSE(3rd sem)

Slide 2: 

CONTENTS “Topics to be covered” FUNCTION in ‘C’ Function in detail syntax and executation Types of Function Calls Call by Value Call by Reference Pointers

Human body: 

Human body We have got various organs in our body for specific tasks. Heart for pumping blood in veins Lungs for breathing Eyes for vision Nose for smell Just think…… Why do we need various organ for specific task….????

Comparison:- HUMAN BODY AND PROGRAMMING: 

Comparison:- HUMAN BODY AND PROGRAMMING As we’ve got heart for pumping blood, lungs for breathing, eyes for vision, ears to hear etc. Similarly in programming we need something to handle our repetitive or specific tasks. And the ‘something’ which handle those tasks is called “ FUNCTION ”

Function concept in mathematics: 

Function concept in mathematics f ( x ) = 5 x - 3 Name of function Parameter of function (x) Function definition When x = 1, f ( x ) = 2 is the returned value. When x = 4, f ( x ) = 17 is the returned value. Returned value is determined by the function definition and by the values of any parameters.

FUNCTION: 

FUNCTION Function is simply a block of statements. That block of statements can be used again and again in a program. Functions divide code into meaningful parts. Variables declared inside a function are local to that function and known only in that function.

Why use functions…???: 

Why use functions…??? Provides an easy way to “Divide and Conquer” problems (especially large ones) into smaller, more manageable sub-tasks Reduces length of the code Reduces complexity Increases efficiency Increases reusability: Write a function once, use it over and over again Abstraction - hide internal details

Slide 8: 

Program for addition of two variables Variable input Arithmetic expression Variable declaration

Slide 9: 

Output of the program value1=1 value2=1 The sum=2

Function syntax: 

Function syntax return-type<function-name>(function parameter) { statements1; statements2; } return type = Tells us about the value which is to be returned. function name = Name of the function. (function parameter) = Declaration of data type to be used. 1 2 3

Functions in detail: 

Functions in detail A function is a program module that performs some particular task. A function has a name that can be used to reference (or call ) the function. The function name is preceded by the return type. The function name is followed by the parameter list in parentheses (sometimes there will be no parameters and so the parentheses will be empty). Parameters Communicate information between functions

Function Execution: 

Function Execution A function call temporarily transfers execution from the calling function to the called function’s code. When the function’s code has finished executing, control is transferred back to the calling function. At that point, execution will return to where it left off at the call If the return type is not void , the function returns a value to the calling place.

Slide 13: 

Program of addition using functions Function declaration Function calling Function definition } Body of function Here [add();] is called function

Slide 14: 

Output of the program a=4 b=4 a+b=8

Passing values between functions: 

Passing values between functions Function declaration Calling function & passing values Function definition Actual arguments Formal arguments Example for pass by value

Slide 16: 

Output of the program a=4 b=4 a+b=8 Enter 3 numbers 6 6 6 Sum=18

Return statement: 

Return statement Return statement sends the control back to the calling function Return statement serves 2 purpose:- On executing the return statement it immediately transfers the control back to the calling program. It returns the value present in the parentheses after return, to the calling function.

Any number of return statements may be used in a program. It may be used anywhere in the program. It returns only one value at one time. (precedence) Valid return statements return ( a ) ; return ( 23 ) ; return ( 12.34 ) ; return ; By default a function returns integer value

Introduction to Pointer : 

Introduction to Pointer Example:- in 640 KB memory i.e. 640 x 1024 bytes=655360 bytes 1 st byte will have address 0,2 nd as 1,3 rd as 2 and so on. Highest address will be 655359. A variable storing memory address is called a pointer as it points to specific memory location.

Two important operators: 

Two important operators & (address of) operator //(Logical operator) * (value at) operator //(Multiplication) main( ) { int i = 3 ; printf ( "\nAddress of i = %u", &i ) ; printf ( "\nValue of i = %d", i ) ; printf ( "\nValue of i = %d", *( &i ) ) ; } Output of program Address of i = 65524 Value of i = 3 Value of i = 3

Declaration of Pointers: 

Declaration of Pointers They are declared like normal variables except for the addition of an unary * character operator. type *<variable name>; Pointer of different types int *iptr; //creates an integer pointer float *ptr; // creates an float pointer char *ptr // creates an char pointer

Initializations of Pointers: 

Initializations of Pointers int i=12; // declares an int variable i int *iptr; // declares an int pointer iptr iptr=&i; // stores the memory address of I into iptr Dereferencing :- To change/access the state of the pointer.

Passing address between functions: 

Passing address between functions Calling function & passing address Function definition Passing address Pointers to hold passed address Example for pass by address Function declaration

Slide 24: 

Output of the program 8 8 Sum=16

Conclusion (1 of 2): 

Conclusion (1 of 2) Function are the best way to reduce efforts in managing the programs. They are reusable We can pass values to any function without changing the actual values. Return statement is used to get back to the calling function and may be used to return some value (only one)

Conclusion (2 of 2): 

Conclusion (2 of 2) To return more than one value to the calling function we can use “CALL BY REFERENCE” by passing address to the called function. If we want to change the value of actual arguments in called function we can use “CALL BY REFERENCE”

Bibliography: 

Bibliography C++, Sumita Arora Let us C, Yashavant P. Kanetkar www.google.com When there is WILL there is Google