C programming functions and definition PDF

Title C programming functions and definition
Author Sachin Dadu
Course Programming Language
Institution University of Calicut
Pages 4
File Size 120.7 KB
File Type PDF
Total Downloads 24
Total Views 159

Summary

c programming funtions and short note to study easy...


Description

FUNCTION There are two type of function in C Language. They are; 

Library function or pre-define function.



User defined function.

Library function Library functions are those which are predefined in C compiler. printf(), scanf(), clrscr(), pow() etc. are pre-defined functions. User defined function These functions are created by programmer according to their requirement. For example to create a function to add two number then you create a function with name sum() this type of function is called user defined function. Function Declarations(Function prototype) A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. Syntax: return_type function_name(parameter); Defining a function. syntax: return_type function_name(parameter) { \\Function body }

Return type: The return_type is the data type of the value the function



returns. Return type parameters and returns statement are optional. Function name: Function name is the name of function it is decided by



programmer. Parameters: This is a value which is passed in function at the time of



calling of function. It is optional. Function body: Function body is the collection of statements.



calling a function When we call any function control goes to function body and execute entire code. For call any function just write name of function and if any parameter is required then pass parameter. Syntax: function_name(); Example #include void sum(); //declaring function int a=10,b=5,c; void sum()

//defining function

{ c = a+b; printf(“sum = %d”, c); } void main() {

sum();// calling function

}

Classification of Functions Based on the parameters and return values, functions can be categorized into four types. They are: 1. Function without arguments and without return value. 2. Function without arguments and with return value. 3. Function with arguments and with return value. 4. Function with arguments and without return value.

1.Function without arguments and without return value In this type of functions there are no parameters/arguments in the function definition and the function does not return any value back to the calling function. Example: void printstars( ) { int i; for(i = 0; i < 20; i++) { printf(“ * ”); } } 2.Function without arguments and with return value In this type of functions, the function definition does not contain arguments. But the function returns a value back to the point at which it was called. An example for this type of function is given below: Example: int readint( ) { int num; printf(“Enter a number: “); scanf(“%d”,&num); return num; } 3.Function with arguments and with return value In

this

type

of

functions,

the

function

definition

consists

of

parameters/arguments. Also, these functions returns a value back to the point at which the function was called. Example: int add(int x, int y) { int result; result = x + y;

return result; } 4. Function with arguments and without return value In

this

type

of

functions,

the

function

definition

consists

of

parameters/arguments but the function does not return any value back to the calling function. Example: void add(int x, int y) { int result; result = x + y; printf(“Result= %d”, result); } Recursive function A function that calls itself is known as a recursive function. And, this technique is known as recursion. #include int fact(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %ld", n, fact(n)); return 0; } int fact(int n) {

if (n >= 1) return (n*fact(n-1)); else return 1;

}...


Similar Free PDFs