Download presentation
Presentation is loading. Please wait.
1
Buy book Online - www.icebreakerspublications.com
3
Func tions Definition :
The function is sub program or set of instructions written to do a particular task. We have already used functions like printf() , scanf() , strlen() etc. Every C program can be thought of as a collection of these functions. There are basically two needs of function: 1. Writing functions avoids rewriting the same code again and again. 2. Using function it becomes easier to write a program. Buy book Online -
4
Example : Function #include<stdio.h> void Hello();
//1. function declaration int main() { Hello(); printf(“I a return 0; } void Hell { printf(“Hello World } Output: Hello World I am back Buy book Online -
5
Types of function call Function call is of two types 1. Call by value 2. Call by reference Call by value: when we call function by passing normal values (or variable), the function call is called as call by value. We have already seen an example of this type above. Call by reference: when we call function by passing reference of variable or values, the function call is called as call by reference. Buy book Online -
6
Cal l by v alue #include<stdio.h> int add (int , int);
//1. function declaration int main() { int y y=Add(5,6); // 2. function call (5,6) are actual argument printf(“addition = %d” , y); return 0; } int add (int x, int y) { // 3. function definition x &y are formal argument int result; result=x+y; return result; } output: addition =11
7
Return Values #include<stdio.h> int add (int , int);
//1. function declaration int main() { int y=Add(5,6); // 2. function call (5,6) are actual argument printf(“addition = %d” , y); return 0; } int add (int x, int y) { // 3. function definition return (x+y); // return (expression) } Buy book Online -
8
Recursion main(); //main() is called again in its own function body
Definition : In C, it is possible for the functions to call themselves. A function is called ‘recursive’ if a statement within the body of a function calls the same function. Output: #include<stdio.h> #include<conio.h> Hello Hello int main() { Hello Hello printf(“\nHello”)͖ main(); //main() is called again in its own function body getch(); return 0; Note: run indefinitely as no } break condition Buy book Online -
9
St r u c t u re A simple variable can hold a single element of any data type. Array allows user to store multiplevalues of same data type. Structure allows user to store multiple values of different data type. Buy book Online -
10
Example : Structure member #include<stdio.h> int main()
struct keyword structure name { st { int pages ; member //structure declaration flo } ; terminating semicolon struct book b1; // sets aside space in memory printf ( "\n Enter no. of pages & price of book\n" ) ; scanf ( " %d %f", &b1.pages, &b1.price ) ; //Accessing member of structure printf ( "\n You entered : " ) ; printf ( "\n %c %f %d", b1.pages, b1.price) ; } //Accessing member of structure Output: Enter no. of pages & price of book You entered
11
End of chapter
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.