Download presentation
Presentation is loading. Please wait.
Published byNeil Hensley Modified over 6 years ago
1
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof
2
Contents Functions Storage classes
3
What is a function? A function is a self-contained subprogram that is meant to do some specific, well-defined task. A program consists of one or more functions. If a program has only one function then it must be the main function.
4
Advantages of functions:
A larger program is divided into sub programs and then solved. When some specific code is to be used more than once at different places in the programs the use of functions avoid repetition of code. Debugging becomes easy. Simple to write the program and can understand what work is done by each part of the program.
5
Types of functions C programs has two types of functions
Library Functions. User defined functions. Library functions: Also called as predefined functions like printf(), scanf(), getchar() etc. User defined Functions: Function that is defined by the user according to his requirement.
6
User defined functions
Users can create their own functions for performing any specific task of the program. These types of functions are called user-defined functions. To create and use these functions, we should know about 1. Function definition 2. Function declaration 3. Function call
7
Example of user defined function
9
HOW TO CALL C FUNCTIONS IN A PROGRAM?
There are two ways that a C function can be called from a program. They are, Call by value Call by reference Types of arguments Actual parameter – This is the argument which is used in function call. Formal parameter – This is the argument which is used in function definition
10
Call by value It is a process of calling a function by passing a normal values as arguments then it is called as call by value. In call by value, the modifications which are performed in a specific function will not effect to its calling area.
11
While Passing Parameters using call by value , xerox copy of original parameter is created and passed to the called function. Any update made inside method will not affect the original value of variable in calling function. In the above example num1 and num2 are the original values and xerox copy of these values is passed to the function and these values are copied into number1,number2 variable of sum function respectively. As their scope is limited to only function so they cannot alter the values inside main function
13
Call by reference It is a process of calling a function by passing the address as arguments. In case of call by reference the modifications which are performed in a specific function will directly effect to its calling area.
14
While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function. Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
16
Recursive function A function which can call itself is called as recursive function. By using recursive functions we can simplify the logic and we can also avoid the loops (while, do while, for). It will repeat until stack is overflow. The recursive function may also leads to the stack overflow.
20
Math Functions Used to perform different mathematical operations on the given data. Generally included in math.h header file. abs(), sqrt(), pow(), ceil(), floor(), log(), log10(), exp(), sin(), cos(), tan().
21
All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below. C function with arguments (parameters) and with return value. C function with arguments (parameters) and without return value. C function without arguments (parameters) and without return value. C function without arguments (parameters) and with return value.
26
Storage classes What is an extent?
The period of time during which memory is associated with a variable is called the extent of the variable. Storage class: It defines the lifetime of variables in a C program. auto static register extern
27
auto An auto variable is a variable which is created automatically when control enters into block and is destroyed when the control leaves the block. The local variables are often referred as automatic variables. Syntax: auto int a;
28
register Register variables are stored in CPU’s registers.
Register variables are faster in execution as they are part of processor. Syntax: register int a;
29
static The static storage class instructs the compiler to keep a local variable in existence during the life- time of the program instead of creating and destroying it each time it comes into and goes out of scope. Syntax: static int a;
30
extern An external variable is a variable defined outside any function block. They are permanent. They are declared out of main function. They are similar to global variables. Syntax : extern int a;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.