Vous êtes sur la page 1sur 14

Function

Function is a self-contained block of statement that perform a task. A C program is a collection of one or more function. If a C program contains only one function, it must be main(). One function can call another function and that function can be called by another one. Function can be called any number of times.

Cont.
The order in which the function are defined in a program and the order in which they get called need not necessarily be same. A function can call itself. Such a process is called Recursion. A function can be called from another function, but a function cannot be defined in main() function. Variable declared in a function are not available to other function in a program.

Cont

Adding too many function and calling them frequently may slow down the program execution. There are basically two types of function: Library function like -Printf () & Scanf (). User-defined functions. Library function always stored in library. Procedure of calling both types of function is exactly same.

Why use Function

To avoid rewriting the same code over and over. To easier write programs and keep track of what they are doing. Separating the code into modular function also makes the program easier to design and understand.

Passing value between functions

The mechanism used to convey information to the function is the argument.


#include <stdio.h> Int calsum (int x, int y, int z); Void main ( ) { Int a, b, c, sum; Printf ( enter any three numbers) Scanf (%d%d%d, & a, &b, &c); Sum= calsum (a, b, c); Printf (sum = %d, sum); }

Int calsum (int x, int y, int z){


Int d; D= x+y+z; Return ( d); }

Variable a, b, c are called actual arguments whereas variables x, y, z are called formal argument.

Array
An array is a collection of similar element, these element could be all ints or all floats or all character etc. An array is a collective name given to a group of similar quantities. Example: percentage [2, 4, 6, 8, 10] First element of an array has zero index. All the data items in array are always stored in consecutive memory locations

An array can always be read or write through loops. Array declaration: Int marks [6]; Number is often called the dimension of an array. It can be initialized: Int marks [ 6 ]= {2, 4, 6, 8,10, 12, 14} The bracket [ ] tells the compiler that we are dealing with an array.

Cont.

Subscript is also known as the size or length of an array and it must be an integer value.

# include <stdio.h> Void main ( ) { Int avg, I, sum=0; Int marks [30]; /* array declaration */ For (i=0; i<=29; i++){ Printf ( enter marks); Scanf (%d, &marks[i]); /* store data in array */ } For(i=0; i<= 29; i++) Sum= sum+marks[i]; /* read data from an array */ Avg= sum / 30; Printf (average marks= %d, avg); }

Function with array


#include <stdio.h> Void display (int); Void main ( ) Int I; Int marks [ ]= { 55, 65,45, 75, 76, 86} For (i=0; i<6; i++) { Display (marks [i]);} Void display ( int m){ Printf (%d, m);}

Library files
Stdio.h which stands for "standard input/output header", . Stdio.h is the header in the C standard library that contains: Macro definitions. Constants. Declarations of functions. Types used for various standard input and output operations. written by Mike Lesk at Bell Labs in the early 1970s.

Math.h is a header file in the standard library of the C programming language designed for basic mathematical operations. Most of the functions involve the use of floating point numbers. C++ also implements these functions for compatibility reasons and declares them in the header cmath.

Conio.h header contains functions for console input/output. Functions of Conio.h can be used to clear screen, change color of text and background, move text, check if a key is pressed or not and many more. Clrscr, delline, getch, wherex, wherey, textcolor, textbackground.

Vous aimerez peut-être aussi