introduction introduction types of function types of function library function library function ...

17
Functions

Upload: emma-perry

Post on 24-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

Functions

Page 2: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

IntroductionTypes of Function Library function User defined function Defining a Function Calling a Function Write a program to add two numbers Advantages of user defined functions

Contents

Page 3: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

IntroductionFunction in programming is a segment that groups a number of

program statements to performspecific task. A C program has at least one function main( ). Without

main() function, there istechnically no C program.

All identifiers in C need to be declared before they are used. This is true for functions as well as

variables. For functions the declaration needs to be before the first call of the function. A full

declaration includes the return type and the number and type of the arguments. This is also called

the function prototype.

A function declaration tells the compiler about a function's name, return type, and parameters. A

function definition provides the actual body of the function.

Back

Page 4: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

Basically, there are two types of functions in C on basis of whether it is defined by user or not.

Library function User defined function

Types of C functions

Back

Page 5: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

Library functions are the in-built function in C programming system.

For example: main() - The execution of every C program starts from this main()

function. printf() - prinf() is used for displaying output in C. scanf() -scanf() is used for taking input in C.

The C standard library provides numerous built-in functions that your program can call.

For example, function strcat() to concatenate two strings, function memcpy() to copy one

memory location to another location and many more functions.

Library function

Back

Page 6: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

C provides programmer to define their own function according to their requirement known as user defined functions. Suppose, a programmer wants to find factorial of a number and check whether it is prime or not in same program.

User defined function

Back

Page 7: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

The general form of a function definition in C programming language is as

follows: return type function name( parameter list ) { body of the function ; }

A function definition in C programming language consists of a function header and a function body. Here are all the parts of a function:

Defining a Function

Page 8: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.

Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.

Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.

Function Body: The function body contains a collection of statements that define what the function does.

Page 9: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

How user-defined function works in C Programming?

#include <stdio.h>void function_name(){ ................ ................ } int main(){ ........... ........... function_name(); ........... ........... }

Page 10: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

As mentioned earlier, every C program begins from main() and program starts executing the codes inside main() function.

When the control of program reaches to function name() inside main() function. The control of program jumps to void function name() and executes the codes inside it.

When, all the codes inside that user-defined function are executed, control of the program jumps to the statement just after function name() from where it is called. Back

Page 11: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.

When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.

Calling a Function

Page 12: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

Example#include <stdio.h> /* function declaration */ int max(int num1, int num2); int main () { /* local variable definition */ int a = 100; int b = 200; int ret; /* calling a function to get max value */ ret = max(a, b);

Page 13: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

printf( "Max value is : %d\n", ret ); return 0; }/* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } Back

Page 14: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

#include<stdio.h>#include<conio.h>float add(float,float);void main()

{float a,b,c;clrscr();printf("Enter the value for a & b\n\n");scanf("%f%f",&a,&b);c=add(a,b);printf("\nc=%f",c);

Write a program to add two numbers

Page 15: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

getch();}float add(float x,float y){float z;z=x+y;return(z);}

Back

Page 16: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

User defined functions helps to decompose the large program into small segments which makes programmer easy to understand, maintain and debug.

If repeated code occurs in a program. Function can be used to include those codes and execute when needed by calling that function.

Programmer working on large project can divide the workload by making different functions.

Advantages of user defined functions

Back

Page 17: Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function

Thank You