Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tejalal Choudhary “C Programming from Scratch” Function & its types

Similar presentations


Presentation on theme: "Tejalal Choudhary “C Programming from Scratch” Function & its types"— Presentation transcript:

1 Tejalal Choudhary “C Programming from Scratch” Function & its types
Declare Call Define “C Programming from Scratch” Function & its types Tejalal Choudhary Department of Computer Science & Engg SDBCT, Indore

2 Contents What is function ? Why function ? Library function
User defined function Header File Creation

3 Scenario 1 Call mechanic

4 Scenario 2 Call electrician

5 Function Self-contained block of statements that perform a specific task. Any C program contains at least one function. One function can call another function When called function has done its thing, control returns to main( ) function nobody is nobody’s boss. function gets called when the function name is followed by a semicolon. A function is defined when function name is followed by a pair of braces A function can be called any number of times. A function can call itself. Function cannot be defined inside another function.

6 Why function? Function avoids rewriting the same code over and over.
Easier to write programs and keep track of what they are doing. Easier to manage and debug

7 printf(), scanf(), clrscr(), getch()
Types of function Function Types Library Functions User Defined Functions printf(), scanf(), clrscr(), getch()

8 Three things about function
Library function User defined function Function declaration No need Need to declare Function definition Need to define Function call Need to call

9 int p=pow(2,3); { statement 1; statement 2; ……. }
Function call: getch(); int p=pow(2,3); Function/Prototype declaration: return-type function-name(arguments list); Function definition return-type function-name(arguments list) { statement 1; statement 2; …… }

10 Actual & formal arguments
Actual arguments These are the actual values passed during function call int a=10, b=20; int r=pow(a,b); Formal arguments The variables used during function definition return-type function-name(int x, int y) { ……. } a & b actual arguments x & y formal arguments

11 Return keyword Return the control to caller Return the value

12 User defined function types
Based on return type and arguments No return & no arguments No return & arguments Return & no arguments Return & arguments void sum(); void sum(int, int); int sum(); int sum(int, int);

13 Scope of function void main( ) { int a = 100 ; printa( a ) ; }
void printa( int b) int c = a+b; printf ( "\n sum=%d", c ) ;

14 Function calling convention
Changes made to the formal arguments in the called function have no effect on actual arguments in the calling function. Call by value When we call function as : int a, b; sum(a,b); //values of a & b will be passed Call by reference


Download ppt "Tejalal Choudhary “C Programming from Scratch” Function & its types"

Similar presentations


Ads by Google