- C Programming
- C Tutorial
- C Basic Syntax
- C Data Types
- C if...else Statement
- C switch Statement
- C for Loop
- C while Loop
- C do...while Loop
- C Jump Statements
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Library Functions
- C Recursion
- C Variable Scope
- C Structures
- C Linked List
- C Stacks
- C Queues
- C Binary Tree
- C Header Files
- C File I/O
- C Programming Examples
- C Programming Examples
C functions with examples
A program's functions are its skeleton. Every C program contains at least one function, the "main()" function. Because a program cannot execute unless it contains the main() function. Because the execution of a C program begins with the main() function, you can declare your own functions (called "user-defined functions"), either in the program itself or in an external file with a ".h" extension called a header file.
Function declaration in C
A function declaration tells the compiler the name of the function, the function's return type, and the function's parameters. The actual body of the function, where the action is performed, is provided by a function definition.
The general form of the function declaration or function prototype is as follows:
return_type function_name(parameter_list);
For example:
int add(int, int);
Function definition in C
Before calling a function, it must be defined. The function definition contains the codes that define the actual task that the function should do when called. The following is the general form of the function definition:
return_type function_name(paramter_list)
{
// body of the function
}
For example:
int add(int m, int n) { int temp; temp = m + n; return temp; }
Function calling in C
To call a function, you only need to write the function name along with its parameter if the function accepts parameters. The following is the general form to call a function in C:
function_name(parameter_list);
For example:
add(x, y);
The return statement in C
The "return" statement terminates a function's execution and returns control to the calling function. The "return" statement will be used in the example that follows this paragraph.
Function example in C
Now let me create a proper example that contains all three steps of a function: the function declaration, the function definition, and the function calling.
#include <stdio.h> int add(int, int); int main() { int x = 10, y = 20, res; res = add(x, y); printf("%d + %d = %d", x, y, res); return 0; } int add(int m, int n) { int temp; temp = m + n; return temp; }
Following is the output produced by this program.
10 + 20 = 30
Since the program execution begins with the "main()" function, inside the "main()", three "int" type variables are declared with 10 and 20 as the initial values of "x" and "y." Now using the statement:
res = add(x, y);
the function "add()" is called. Now the compiler checks whether the prototype of the function "add()" is available in the program or not. If it is available, then it checks whether it accepts two "int" type parameters or not. Because both conditions are met, the program flow proceeds to the function definition section to determine what this function is for or what its task is.
Inside the function definition, the values of "x" and "y" are copied to the formal parameters "m" and "n." Within the function definition, another "temp" variable of type "int" is declared. Now the values of "m" and "n" are added and initialized to the "temp" variable. And the value of the "temp" variable is returned to the function call.
Therefore, the program flow goes back to the "main()" function at the same position from where it came to the function definition. Therefore, the "30," the addition of two numbers, will be initialized to "res," whose value is printed on the output console using the "printf()" function.
Since there is a return value for the function "add()," the function must return an int type value. However, the above program can also be created in this way:
#include <stdio.h> int add(int, int); int main() { int x = 10, y = 20; printf("%d + %d = %d", x, y, add(x, y)); return 0; } int add(int m, int n) { return (m+n); }
You can also create a function that does not return any value. As an example:
#include <stdio.h> void add(int, int); int main() { int x = 10, y = 20; add(x, y); return 0; } void add(int m, int n) { printf("%d + %d = %d", m , n, m+n); }
Call by value and call by reference in C
When we call the function by value, the actual value is copied to the formal parameters, which means that any change to the parameter within the function definition has no effect on the original value. If we call the function by reference, any change in the function's parameter will reflect the change to the original value. Consider the following C call by value approach for calling a function:
#include <stdio.h> void swap(int, int); int main() { int x = 10, y = 20; printf("Before swap (inside main()): \n"); printf("'x' = %d \t\t 'y' = %d \n\n", x, y); swap(x, y); printf("After swap (inside main()): \n"); printf("'x' = %d \t\t 'y' = %d \n", x, y); return 0; } void swap(int m, int n) { int temp; temp = m; m = n; n = temp; printf("After swap (inside swap()): \n"); printf("'m (x)' = %d \t\t 'n (y)' = %d \n\n", m, n); }
The output is:
Before swap (inside main()): 'x' = 10 'y' = 20 After swap (inside swap()): 'm (x)' = 20 'n (y)' = 10 After swap (inside main()): 'x' = 10 'y' = 20
You can see that the swap performed inside the "swap()" method does not swap the original value. Now let me implement the same program with the call-by-reference approach:
#include <stdio.h> void swap(int *, int *); int main() { int x = 10, y = 20; printf("Before swap (inside main()): \n"); printf("'x' = %d \t\t 'y' = %d \n\n", x, y); swap(&x, &y); printf("After swap (inside main()): \n"); printf("'x' = %d \t\t 'y' = %d \n", x, y); return 0; } void swap(int *m, int *n) { int temp; temp = *m; *m = *n; *n = temp; printf("After swap (inside swap()): \n"); printf("'m (x)' = %d \t\t 'n (y)' = %d \n\n", *m, *n); }
Now this program produced the following output:
Before swap (inside main()): 'x' = 10 'y' = 20 After swap (inside swap()): 'm (x)' = 20 'n (y)' = 10 After swap (inside main()): 'x' = 20 'y' = 10
That is, I declared the parameters of the function "swap()" for a pointer type variable, which can store both addresses and values. The "&" is called the "address of," where "*" is called the "value at address" operator.
I passed the addresses of "x" and "y" while calling the function "swap()." Therefore, the addresses of these two variables are initialized to "m" and "n," the formal parameters. Since the swapping was performed on the address, any change in the function definition reflects the change in the actual value inside the main() function.
« Previous Tutorial Next Tutorial »