






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Function in c programming detailed
Typology: Study notes
1 / 11
This page cannot be seen from the preview
Don't miss anything!







A function is a group of statements that together perform a task. Every C program has at least one function, which is main (), and all the most trivial programs can define additional functions. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions. A function can also be referred as a method or a sub-routine or a procedure, etc. Defining a Function The general form of a function definition in C programming language is as follows – return_type function_name(parameter list ) { body of the function } A function definition in C programming consists of a function header and a function body. Here are all the parts of a function – Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body − The function body contains a collection of statements that define what the function does. Example Given below is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum value between the two –
/* function returning the max between two numbers / int max(int num1, int num2) { / local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; } Function Declarations A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration has the following parts – return_type function_name( parameter list ); For the above defined function max(), the function declaration is as follows – int max(int num1, int num2); Parameter names are not important in function declaration only their type is required, so the following is also a valid declaration – int max(int, int); Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function. Function prototype A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. A function prototype gives information to the compiler that the function may later be used in the program. Syntax of function prototype returnType functionName(type1 argument1, type2 argument2,...); In the above example, int add Numbers (int a, int b); is the function prototype which provides following information to the compiler:
In this category, functions have some arguments and it receives data from the calling function. Similarly, it returns a value to the calling function. The calling function receives data from the called function. So, it is two-way data communication between calling and called functions. Eg. Factorial of a Number #include #include int fact(int); void main() { int n; printf("\n Enter n:"); scanf("%d",&n); printf("\n Factorial of the number : %d", fact(n)); } int fact(int n) { int i,f; for(i=1,f=1;i<=n;i++) { f=f*i; } return(f); } Functions with no arguments and return values. In this category, the functions have no arguments and it doesn’t receive any data from the calling function, but it returns a value to the calling function. The calling function receives data from the called function. So, it is one way data communication between calling and called functions. Eg. Sum of Numbers #include #include int sum(); void main() { int s; printf("\n Enter number of elements to be added :"); s=sum(); printf("\n Sum of the elements :%d",s); } i nt sum() { int a[20], i, s=0,n; scanf("%d",&n); printf("\n Enter theelements:"); for(i=0;i< n; i++) { scanf("%d",&a[i]);
for(i=0;i< n; i++) { s=s+a[i]; } return s; } Function Calls This calls the actual function Syntax: function_name (arguments list); There are two ways that a C function can be called from a program. They are,
int temp; temp = *num1; *num1 = *num2; *num2 = temp; } int main() { int num1=50,num2=70; interchange(&num1,&num2); printf("\nNumber 1 : %d",num1); printf("\nNumber 2 : %d",num2); return(0); } Output : Number 1 : Number 2 : Call by Address 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. Difference between Call by Value & Call by Reference Call by Value Call by Reference A copy of the value is passed into the function An address of value is passed into the function Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters. Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters. Actual and formal arguments are created at the different memory location Actual and formal arguments are created at the same memory location
Every Variable in a program has memory associated with it. Memory Requirement of Variables is different for different types of variables. In C, Memory is allocated & released at different places Terms Definition Scope Region or Part of Program in which Variable is accessible
Extent Period of time during which memory is associated with variable Storage Class Manner in which memory is allocated by the Compiler for Variable Different Storage Classes Storage class of variable Determines following things Where the variable is stored Scope of Variable Default initial value Lifetime of variable Where the variable is stored: Storage Class determines the location of variable, where it is declared. Variables declared with auto storage classes are declared inside main memory whereas variables declared with keyword register are stored inside the CPU Register. Scope of Variable Scope of Variable tells compile about the visibility of Variable in the block. Variable may have Block Scope, Local Scope and External Scope. A scope is the context within a computer program in which a variable name or other identifier is valid and can be used, or within which a declaration has effect. Default Initial Value of the Variable Whenever we declare a Variable in C, garbage value is assigned to the variable. Garbage Value may be considered as initial value of the variable. C Programming have different storage classes which has different initial values such as Global Variable have Initial Value as 0 while the Local auto variable have default initial garbage value. Lifetime of variable Lifetime of the = Time Of variable Declaration - Time of Variable Destruction Suppose we have declared variable inside main function then variable will be destroyed only when the control comes out of the main .i.e end of the program. Different Storage Classes: Auto Storage Class Static Storage Class Extern Storage Class Register Storage Class Automatic (Auto) storage class This is default storage class. All variables declared are of type Auto by default.
display(); } void display() { extern int num ; printf("nNum : %d",num); } Static Storage Class 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. Therefore, making local variables static allows them to maintain their values between function calls. The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared. In C programming, when static is used on a class data member, it causes only one copy of that member to be shared by all the objects of its class. #include<stdio.h> void func(void); static int count = 5; /* global variable / main() { while(count--) { func(); } return 0; } void func( void ) { static int i = 5; / local static variable*/ i++; printf("i is %d and count is %d\n", i, count); } Register Storage Class register keyword is used to define local variable. Local variables are stored in register instead of RAM. As variable is stored in register, the Maximum size of variable = Maximum Size of Register unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register. This is generally used for faster access. Example
#include<stdio.h> int main() { int num1,num2; register int sum; printf("\nEnter the Number 1 : "); scanf("%d",&num1); printf("\nEnter the Number 2 : "); scanf("%d",&num2); sum=num—num2; printf("\nSum of Numbers :%d",sum); return(0); }