Function in C
-
Upload
abhineet-anand -
Category
Education
-
view
1.371 -
download
4
description
Transcript of Function in C
Function
Abhineet Anand
Center For Information TechnologyCollege of Engineering Studies
UPES Dehradun, India
November 18, 2013
Abhineet Anand Function
Function
Definition
A Function is a self-contained block of statement that perform a coherenttask of some kind.
1 Every C Program can be thought of as a collection of these functions.
2 Function is a set of instruction to carryout a particular task.
3 Function after its execution returns a single value.
4 Generally, the function are classified into standard function anduser-defined functions.
5 The Standard function are also called library function or built-infunction.
6 All standard function, such as sqrt(), abs(), log(), sin() etc. areprovided in the library of function.
7 But, most of the application need other functions than those availablein the software, those are known as user-defined functions.
Abhineet Anand Function
Function
Definition
A Function is a self-contained block of statement that perform a coherenttask of some kind.
1 Every C Program can be thought of as a collection of these functions.
2 Function is a set of instruction to carryout a particular task.
3 Function after its execution returns a single value.
4 Generally, the function are classified into standard function anduser-defined functions.
5 The Standard function are also called library function or built-infunction.
6 All standard function, such as sqrt(), abs(), log(), sin() etc. areprovided in the library of function.
7 But, most of the application need other functions than those availablein the software, those are known as user-defined functions.
Abhineet Anand Function
Function
Definition
A Function is a self-contained block of statement that perform a coherenttask of some kind.
1 Every C Program can be thought of as a collection of these functions.
2 Function is a set of instruction to carryout a particular task.
3 Function after its execution returns a single value.
4 Generally, the function are classified into standard function anduser-defined functions.
5 The Standard function are also called library function or built-infunction.
6 All standard function, such as sqrt(), abs(), log(), sin() etc. areprovided in the library of function.
7 But, most of the application need other functions than those availablein the software, those are known as user-defined functions.
Abhineet Anand Function
Need of Function
Several advantages of modularizing a program into function includes:
Reduction in code redundancy
Enabling code reuse
Better readability
Information Hiding
Improved debugging and testing
Improved maintainability
Function interact with each other to accomplish a particular task. Theyare classified according to the following criteria:
Based upon who develop the function
Based upon the number of arguments a function accepts.
Abhineet Anand Function
Need of Function
Several advantages of modularizing a program into function includes:
Reduction in code redundancy
Enabling code reuse
Better readability
Information Hiding
Improved debugging and testing
Improved maintainability
Function interact with each other to accomplish a particular task. Theyare classified according to the following criteria:
Based upon who develop the function
Based upon the number of arguments a function accepts.
Abhineet Anand Function
Need of Function
Several advantages of modularizing a program into function includes:
Reduction in code redundancy
Enabling code reuse
Better readability
Information Hiding
Improved debugging and testing
Improved maintainability
Function interact with each other to accomplish a particular task. Theyare classified according to the following criteria:
Based upon who develop the function
Based upon the number of arguments a function accepts.
Abhineet Anand Function
Classification of Functions
User-defined function
Defined by user at the time of writing the program.
There are three aspect of working with user-defined functions:
Function Declaration, also known as function prototypeFunction DefinitionFunction use, also known as function call or function invocation
Declaration
Introduces the function name, function return type, and functionparameters to the program.
The function body (statements)is not part of the declaration
A function must be declared before it is used
return_type function_name(parameter_list);
parameter_list: type param1,type param2,type param3, etc
double sqrt(double);
int func1();
void func2(char,int);
Abhineet Anand Function
Classification of Functions
User-defined function
Defined by user at the time of writing the program.
There are three aspect of working with user-defined functions:
Function Declaration, also known as function prototypeFunction DefinitionFunction use, also known as function call or function invocation
Declaration
Introduces the function name, function return type, and functionparameters to the program.
The function body (statements)is not part of the declaration
A function must be declared before it is used
return_type function_name(parameter_list);
parameter_list: type param1,type param2,type param3, etc
double sqrt(double);
int func1();
void func2(char,int);
Abhineet Anand Function
Classification of Functions
User-defined function
Defined by user at the time of writing the program.
There are three aspect of working with user-defined functions:
Function Declaration, also known as function prototypeFunction DefinitionFunction use, also known as function call or function invocation
Declaration
Introduces the function name, function return type, and functionparameters to the program.
The function body (statements)is not part of the declaration
A function must be declared before it is used
return_type function_name(parameter_list);
parameter_list: type param1,type param2,type param3, etc
double sqrt(double);
int func1();
void func2(char,int);Abhineet Anand Function
Function Definition
Function Definition
Function Definition, also known as function implementation, meanscomposing a function. Every Function defination consists of two Parts:
Header of the function,
Body of the function.
return_type function_name(parameter_list){
// Function body }
Abhineet Anand Function
Function Definition
Function Definition
Function Definition, also known as function implementation, meanscomposing a function. Every Function defination consists of two Parts:
Header of the function,
Body of the function.
return_type function_name(parameter_list){
// Function body }
Abhineet Anand Function
Function Definition
Function Definition
The general form of header of a function is:
[return_type] function_name([parameter_list])
The header of function is not terminated with a semicolon.
The body of a function consist of a set of statements enclosedwithin braces.
The return statement is used to return the result of the computationsdone in the called function and/or to return the program control backto the calling function.
A function can be defined in any part of the program text or within alibrary.
void print_func(){cout << hello world << endl;}
Abhineet Anand Function
Function Invocation/call/Use
Function Invocation/call/Use
Depending upon their inputs (i.e. parameters) and outputs, functions areclassified as:
Function with no input-output.
Function with inputs and no output.
Function with input and one output.
Function with input and output.
Abhineet Anand Function
Function with no Input-Output
Function with no Input-Output
A function with no input-output does not accept any input and does notreturn any result.
//Function with no input-output
#include<stdio.h>
printsum(); //Function deceleration
main() //main function, the master function
{
printsum(); //Function Calling
}
printsum() //definition of function printsum
{
printf("Sum of 2 and 3 is %d",2+3);
}
Abhineet Anand Function
Function with Input and No Output
Function with Input and No Output
A function can be made flexible by adding input to it.
//Function with input and No output
#include<stdio.h>
void printsum(int, int); //Function deceleration
main() //main function, the master function
{
int a,b;
printf("Enter values of a & b\t");
scanf("%d %d", &a,&b);
printsum(a,b); //Function Calling
}
printsum(int x, int y) //definition of function printsum
{
printf("Sum of %d and %d is %d",x,y,x+y);
}
Abhineet Anand Function
Function with Input and No Output
Function with Input and No Output
The expression that appear within parenthesis of function call areknown as actual arguments.
The variable declared in the parameter list in the function header areknown as formal parameters.
The below-mentioned steps are followed when a function with input isinvoked:
1 The Actual arguments expression are evaluated.2 The program control is transfered to the called function and the result
of the evaluation of the actual argument expression are assigned to theformal parameters on one-to-one basis.
3 The execution of the calling function is suspended and the calledfunction starts execution.
Abhineet Anand Function
More Generalization of the previous example
//Function with three input and No output
#include<stdio.h>
void printsum(int, int, char); //Function deceleration
void main() //main function, the master function
{
int a,b; char base;
printf("Enter values of a & b\t");
scanf("%d %d", &a,&b);
printf("Enter base of output(O, D or H)\t");
flushall();
scanf("%c", &base);
printsum(a, b, base); //Function Calling
}
Abhineet Anand Function
More Generalization of the previous example
void printsum(int x, int y, char base) //definition of function printsum
{
if(base=’O’)
printf("Sum of %d and %d in octal is %o",x,y,x+y);
if(base=’D’)
printf("Sum of %d and %d in decimal is %d",x,y,x+y);
if(base=’H’)
printf("Sum of %d and %d in hexadecimal is %X",x,y,x+y);
}
Abhineet Anand Function
Function with Input and One Output
Function with Input and One Output
The Result of the computation may be required in the calling functionfor further processing. The best software engineering practicessuggest the following:
1 The developed functions should be kept as general as possible so thatthey can be used in different situations.
2 A function should receive inputs in the form of arguments and returnthe result of computation instead of directly printing it. A functionshould behave like a ’black box’ that receive inputs, and outputs thedesired value.
return statement
The return statement is used to return the result of the computationsperformed in the called function and/or transfer the program controlback to the calling function.
There are two forms of the return statement:1 return,2 return expression.
Abhineet Anand Function
Function with Input and One Output
Function with Input and One Output
The Result of the computation may be required in the calling functionfor further processing. The best software engineering practicessuggest the following:
1 The developed functions should be kept as general as possible so thatthey can be used in different situations.
2 A function should receive inputs in the form of arguments and returnthe result of computation instead of directly printing it. A functionshould behave like a ’black box’ that receive inputs, and outputs thedesired value.
return statement
The return statement is used to return the result of the computationsperformed in the called function and/or transfer the program controlback to the calling function.
There are two forms of the return statement:1 return,2 return expression.
Abhineet Anand Function
Function with Input and One Output
Abhineet Anand Function
Function with Input and Output
Function with Input and Output
More than one value can be indirectly returned to the calling functionby making the use of Pointer.
Pointers can also be used to pass arguments to a function.
Depending upon whether the values or addresses(i.e. pointer) arepassed as arguments to a function, the argument passing methods inC language are classified as:
1 Pass by value,2 Pass by address
Abhineet Anand Function
Function with Input and Output
Passing Arguments by Value
The method of passing arguments by value is also known as call byvalue.
The value of actual argument are copied to the formal parameters ofthe function.
If the argument are passed by value, the change made in the values offormal parameters inside the called function are not reflected back tothe calling function.
Abhineet Anand Function
Function with Input and Output
Abhineet Anand Function
Function with Input and Output
Passing Arguments by Address/Reference
The method of passing arguments by address or reference is alsoknown as call by Address or Call by reference.
The address of the actual arguments are passed to the formalparameters of the function.
If, the arguments are passed by reference, the change made in thevalues pointed to by the formal parameter in the called function arereflected back to the calling function.
Abhineet Anand Function
Function with Input and Output
Abhineet Anand Function
Function with Input and Output
Abhineet Anand Function
Passing Arrays to function
Passing Arrays to function
Like simple variable, arrays can also be passed to functions.
There are two ways to pass array to function:
Passing individual elements of an array one by one.Passing an Entire array at a time.
Passing individual element
Passing individual element of an array one by one is similar to passingbasic variables.
The individual elements of an array can be passed either by value orby reference.
Passing entire array at a time
Passing entire array at a time is preferred way of passing arrays tofunctions. The entire array is always passed by reference.
Abhineet Anand Function
Passing Arrays to function
Passing Arrays to function
Like simple variable, arrays can also be passed to functions.
There are two ways to pass array to function:
Passing individual elements of an array one by one.Passing an Entire array at a time.
Passing individual element
Passing individual element of an array one by one is similar to passingbasic variables.
The individual elements of an array can be passed either by value orby reference.
Passing entire array at a time
Passing entire array at a time is preferred way of passing arrays tofunctions. The entire array is always passed by reference.
Abhineet Anand Function
Passing Arrays to function
Passing Arrays to function
Like simple variable, arrays can also be passed to functions.
There are two ways to pass array to function:
Passing individual elements of an array one by one.Passing an Entire array at a time.
Passing individual element
Passing individual element of an array one by one is similar to passingbasic variables.
The individual elements of an array can be passed either by value orby reference.
Passing entire array at a time
Passing entire array at a time is preferred way of passing arrays tofunctions. The entire array is always passed by reference.
Abhineet Anand Function