Why we use Functions in C or what is the C function?
A function is a block of code that only runs when it is called.it will not automatically run on run time like other if statements and tells us the result. You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times as you want it to lessen the number of lines of code and make code more efficient and readable and faster and more understandable.
Benefits of Functions:
- By using functions, we can avoid rewriting the same logic/code again and again in a program.
- We can call C functions any number of times in a program and from any place in a program.
- We can track a large C program easily when it is divided into multiple functions.
- Reusability is the main achievement of C functions.
- However, Function calling is always overhead in a C program.
How to prototype a Function?
- A function prototype is a declaration of the function that tells the program about the type of value returned by the function and the number and type of arguments.
- Function prototyping is one very useful feature of C function. A function prototype describes the functional interface to the compiler by giving details such as the number and type of arguments and the type of return values.
- The prototype declaration looks just like a function definition except that it has no body i.e., its code is missing. This is the time you knew the difference between a declaration and a definition.
- A declaration introduces a (function) name to the program whereas a definition is a declaration that also tells the program what the function is doing and how it is doing.
- Thus, the above-given examples are function definitions and the following are declarations or shall we say function prototypes:
Example of the prototype function in C
int functiona(int a);
// this is function of type int return type
int funb(int n1, int n2);
How to send data into a Function?
Once we declared the function, we can send parameters into the function to do more tasks. In the example below int, a, and int n1 and int n2 are the data that can be sent into the functions.
Example:
int a(int a);
int b(int n1, int n2);
Passing Data by Value in C
Incall by value, the actual value that is passed as an argument is not changed after performing some operation on it. When call by value is used, it creates a copy of that variable into the stack section in memory. When the value is changed, it changes the value of that copy, the actual value remains the same.
Example of Call by value in C
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
Call by Reference in C
In incall by reference, the actual value that is passed as an argument is changed after performing some operation on it. When a call by reference is used, it creates a copy of the reference of that variable into the stack section in memory.
Is uses a reference to get the value? So, when the value is changed using the reference it changes the value of the actual variable.
Example of Call by Reference in C
#include <stdio.h>
/* function declaration */
void swap(int *x, int *y);
int main () {
/* local variable definition */
int a = 100;
int b = 200;
printf("Before swap, value of a is as below : %d\n", a );
printf("Before swap, value of b is as below: %d\n", b );
swap(&a, &b);
printf("After swap, value of a is changing : %d\n", a );
printf("After swap, value of b is changing : %d\n", b );
return 0;
}
Output on screen:
Before swap, value of a is as below :100
Before swap, value of b is as below :200
After swap, value of a is changing :200
After swap, value of a is changing :100
Tips for this coding:
- The call by reference is mainly used when we want to change the value of the passed argument into the invoker function.
- One function can return only one value. When we need more than one value from a function, we can pass them as an output argument in this manner.
The return Statement
The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point, regardless of whether it’s in the middle of a loop, etc.
Example of the return statement
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}//end max
How to Return a Value from a Function?
We can also return value from a function at the end of the function.
Example of return from function in C
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
int r = a+b;
return r; // this is the return value from a function
}//end max
How to return a Boolean Value from a function in C?
Functions may return true or false values. In the first case that is when C is used it is better to use return type bool.
Local and Global Variables in C
- Variable Scope in C Inside a function or a block which is called local variables, the variables which are declared outside of all the function and accessible from all functions including the main function are known as Global variables.
- Consider the program: In this example, we have shown how global and local variable behaves and to manipulate them.
Example of Local and Global Variable in C
#include
int main ()
{
// this is local variable
int a, b;
int c;
a = 15;
b = 20;
c = a + b;
printf ("local a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
Output on screen:
Local a = 15, b = 20, c = 35
What are static Local Variables in C Function?
Static variables in a Function: When a variable is declared as static, space for it gets allocated for the lifetime of the program.
Even if the function is called multiple times, space for the static variable is allocated only once and the value of the variable in the previous call gets carried through the next function call.
Example of a static local variable in C
#include<stdio.h>
int fun()
{
static int count = 0;
count++;
return count;
}
int main()
{
printf("%d ", fun());
printf("%d ", fun());
return 0;
}
Output on screen:
1 2
What are Default Arguments in C
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn’t provide a value for the argument with a default value.
Following is a simple C example to demonstrate the use of default arguments. We don’t have to write 3 sum functions, only one function works by using default values for 3rd and 4th arguments.
Example of Default Arguments in C
int x(int = 1, int); // error, assuming there's no previous declaration of x
void f(int n, int k = 1);
void f(int n = 0, int k); // OK: k's default supplied by previous decl in the same scope
void g(int, int = 7);
void h() {
void g(int = 1, int); // Error: not the same scope
}
How to exit from Function?
The exit () Function- Terminates the process normally, performing the regular clean-up for terminating programs. Normal program termination performs the following (in the same order):
- Objects associated with the current thread with thread storage duration are destroyed (C only).
- Objects with static storage duration are destroyed (C) and functions registered with atexit are called.
- All C streams (open with functions in <cstdio>) are closed (and flushed, if buffered), and all files created with tmpfile are removed.
- Control is returned to the host environment.
This statement can be used as an exit statement in C
void exit(int status);
How to clear the Screen
The following command can be used to clear the screen on the window
System(“pause”);