Slide 1: Functions
The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Library User define
function function Slide 2: Library function:
The library function are not required to be written by us.
scanf Slide 3: User define function:
This function as to be developed by the user at the time of writing a program.
‘main’ is specially used function in C. Ever program must have main function to indicate, where the program begins its execution. When a program to large and complex then the result of debugging testing and maintaining becomes difficult. Slide 4: Advantages:
It felicitate top down modular program.
The length of the source program can be reduce by using functions at
It is easy to locate and isolate and fault function easily
A function can used by many other program, it means programmer built an what have already done, insert of starting over scratch. Slide 5: Main program
Function 1 function 2 function3 function4 Using Functions : Using Functions The main functions and other library functions does need to be declared and defined but the main function’s body need to be defined by the programmer. The 3 components associated with functions are: : The Declaration
The function definition
The Calling Statement The 3 components associated with functions are: Function Declaration : In C user- written functions should normally be declared prior to its use to allow compiler to perform type checking on arguments used in its call statement.
The general form is:
Retirn_data_type function_name (data_type Var_name, …..); Function Declaration Slide 9: Function name: this is the name given to the function. It follows the same naming convention as that of any valid variable in C.
Return data type: this specifies the type of data given back to the calling construct.
Data type list: this list specifies the data type of each variables, the values of which are expected to be transmitted to the function. These variables are known as formal parameters. Note: : It is possible not to declare functions prior to the use but the error checking will not be performed.
; is required at the end of function declaration.
E.g. int FindMax(int x, int y); Note: Function Definition : The collection of program statements that does a specific tasks done by the function is called the function definition.
It conist of function header:
Int FindMax(int x, int y)
and function body.
Int FindMax(int x, int y)
//body of the function….
} Function Definition Function call : The function is called from the main()
The function can in turn call a another function.
the function call statements invokes the function, which means the program control passes to that function. Once the function completes its task, the program control is passed back to the calling environment. Function call Slide 13: The general form of calling stmt is:
Function_name (var1, var2,..);
var_name=function name(var1, var2,..); Salient points to be taken into consideration : The func name and the type and number of arguments must match with that of the function declaration stmt and the header of the function definition.
Arguments present in the form of expression are evaluated and converted to the type of formal parameters at the beginning of the body of the function. Salient points to be taken into consideration Passing of arguments to the function : Call by value or pass by value:
When arguments are passed by values this means that local copies of the values of the arguments are passed to the function.
Call by reference or pass by reference.
The address of the variable is passed as value of parameters to the function. Passing of arguments to the function Slide 16: E.g.
int sum(int x, int y)
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
} Slide 17: Program:
} Passing arrays to functions : Passing arrays to functions Arrays can also be the arguments of function
Only the base address of the array is passed to the function
Hence the passing of arguments is done by reference.
When arrays is passed as arguments then actual contents of the arrays is altered. Slide 19: #include<stdio.h>
Int I, b;
Printf(“enter age of stud in team”);
Average=avg_age(b); Slide 20: Printf((“the avg is %d”,average);
Float avg_age(int a)
} Scope of variables : Scope of variables The scope rules related to functions basically relate to the accessibility, duration of existence, and the boundary of the usage of the variables declared within the block.
Global scope Storage Classes : Storage Classes Storage classes specify the location where the variables will be stored: primary memory, registers of cpu of the computer.
The general form is:
<storage_class_spesifier> <data_type> <variable_name>; Types of storage classes : Types of storage classes Automatic
ststic The automatic storage class : The automatic storage class The variables declared within the body of variables are automatic
Even if we do not include the keyword in function while declaring variables they are implicitly defined as automatic
The auto is used to explicitly define a variable as automatic.
These variables are stored in primary memory.
This keyword is rarely used External storage class : External storage class The keyword extern is used.
It used to declare global variables which are accessible within different files and functions.
The default value of such variable is zero.
It is stored in primary memory. Slide 26: //pgm1.c
} Slide 27: //pgm2.c
Extern int I;
} The Register Storage Class : The Register Storage Class The variables stored in register of CPU are accessed in much lesser time.
The keyword register is used.
The default value is not known
It is applicable to int and char only.
The scope is limited upto the region or block or function in which it is defined. Static Storage class : Static Storage class The static storage class variable makes the variable permanent.
Their can be local i.e. internal static variables and global i.e. external static variables.
The default value is zero.
They are stored in primary memory. Slide 30: The external static variables in a program are declared like global variables with the keyword static.
The static var are accessible by all functions in the program file where these variables exist and are declared. These variables are not accessible in the other files.
These variables exist thru out the main program execution. Slide 31: #include<stdio.h>
Printf(“\n first call”)
Printf(“\n 2nd call”)
Printf(“\n third call”)
} Slide 32: Void show()
Static int i;
First call i=0; 2nd call i=1 3rd call i=2 Recursive Functions. : Recursion in programming is a technique for defining a problem in terms of one or more smaller versions of the problem.
A recursive function is one that calls itself directly or indirectly to solve a smaller version of its task until a final call which does not require a self call. Recursive Functions. Slide 34: #include<stdio.h>
Printf(“Enter a character (‘.’) to end”);
If(ch != ‘.’)
i/p : hi o/p: ih The mechanics of recursive call : Start main program
1st call to print backward
enter a char : H
2nd call to print_backward
enter a char: i.
3rd call to print_backward
enter a char: .
//now it will not call againcoz its ‘.’
3rd call finish
2nd call finish
First call Finish…
End of main program……… The mechanics of recursive call How recursion is implemented. : The storage mechanism in most modern languages is stack storage mgmt.
In this mechanism the program’s data area is allocated at load time.
While storage for functions data is allocated when function is invoked.
On exit from function this storage is de-allocated.
This results in a runtime stack.
In recursive cases one call does not overlap with the other. How recursion is implemented. What is need for implementing recursion : Decomposition into smaller problems of same size.
Recursive call must diminish problem size.
Necessity of base case.
Base case must be reached. What is need for implementing recursion What is a base case. : An instance of a problem solution of which requires no further recursive calls is known as base case.
It is special case whose solution is known.
Every recursive algo requires at least one base case in order to be valid. What is a base case. The purpose of base case : It terminates the condition. Without an explicitly defined base case, a recursive function would call itself indefinitely. The purpose of base case Program to find factorial of a number using recursive function. : #include<stdio.h>
Long int factorial(int no);
Printf(“factorial of %d is %ld”, I, factorial(i));
} Program to find factorial of a number using recursive function. Slide 41: Long int factorial(int n)
Return (n * factorial(n-1));
o/p: 6 Slide 42: N->3
Ans = 3* factorial (3-1)//2
Ans =1 * factorial (1-1)//0
Return 6. Comparing recursion and iteration : Recursion is like top-down approach to problem solving.
Iteration is more bottom up approach
Recursion can require substantial amount of overhead. Comparing recursion and iteration