ICT3 Function

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

FUNCTIONS

Chapter 6: Advanced Programming


Functions in C are the basic building blocks of a C program. A
function is a set of statements enclosed within curly brackets ({})
that take inputs, do the computation, and provide the resultant
output. You can call a function multiple times, thereby allowing
reusability and modularity in C programming. It means that instead
of writing the same code again and again for different arguments,
you can simply enclose the code and make it a function and then
call it multiple times by merely passing the various arguments.
WHY DO WE NEED FUNCTIONS IN C PROGRAMMING?

We need functions in C programming and even in other


programming languages due to the numerous advantages they
provide to the developer. Some of the key benefits of using
functions are:

• Enables reusability and reduces redundancy


• Makes a code modular
• Provides abstraction functionality
• The program becomes easy to understand and manage
• Breaks an extensive program into smaller and simpler pieces
BASIC SYNTAX OF FUNCTIONS
The basic syntax of functions in C programming is:

return_type function_name(arg1, arg2, … argn)

Body of the function //Statements to be processed

}
In the syntax:
return_type: Here, we declare the data type of the value returned by functions.
However, not all functions return a value. In such cases, the keyword void
indicates to the compiler that the function will not return any value.
function_name: This is the function’s name that helps the compiler identify it
whenever we call it.
arg1, arg2, ...argn: It is the argument or parameter list that contains all the
parameters to be passed into the function. The list defines the data type,
sequence, and the number of parameters to be passed to the function. A
function may or may not require parameters. Hence, the parameter list is
optional.
Body: The function’s body contains all the statements to be processed and
executed whenever the function is called.
Note: The function_name and parameters list are together known as the
signature of a function in C programming.
ASPECTS OF FUNCTIONS IN C PROGRAMMING
Functions in C programming have three general aspects: declaration, defining, and
calling. Let’s understand what these aspects mean.
1. Function Declaration
The function declaration lets the compiler know the name, number of parameters, data
types of parameters, and return type of a function. However, writing parameter names
during declaration is optional, as you can do that even while defining the function.
2. Function Call
As the name gives out, a function call is calling a function to be executed by the
compiler. You can call the function at any point in the entire program. The only thing to
take care of is that you need to pass as many arguments of the same data type as
mentioned while declaring the function. If the function parameter does not differ, the
compiler will execute the program and give the return value.
3. Function Definition
It is defining the actual statements that the compiler will execute upon calling the
function. You can think of it as the body of the function. Function definition must return
only one value at the end of the execution.
Here’s an example with all three general aspects of a function.
#include <stdio.h>
// Function declaration
int max_Num(int i, int j){
// Function definition
if (i > j)
return i;
else
return j;
}
// The main function.
int main(void){
int x = 15, y = 20;
// Calling the function to find the greater number
among the two
int m = max_Num(x, y);
printf("The bigger number is %d", m);
return 0;
}
TYPES OF FUNCTIONS IN C PROGRAMMING
Functions in C programming are classified into two types:
1. Library Functions
Also referred to as predefined functions, library functions are already defined in
the C libraries. This means that we do not have to write a definition or the
function’s body to call them. We can simply call them without defining them as
they are already defined. However, we need to include the library at the
beginning of the code for calling a library function. We can then use the proper
syntax of the function to call them. Printf(), scanf(), ceil(), and floor() are
examples of library functions.
2. User-Defined Functions
These are the functions that a developer or the user declares, defines, and calls
in a program. This increases the scope and functionality, and reusability of C
programming as we can define and use any function we want. A major plus point
of C programming is that we can add a user-defined to any library to use it in
other programs.
HEADER FILES FOR LIBRARY FUNCTIONS IN C PROGRAMMING

As mentioned earlier, all library functions are included in different header files
saved with a .h extension. To use any library functions, you need to use the
header files at the beginning of the program. Without including the header files,
the program will not be executed as the compiler will throw errors. Here are the
header files available in C.
DIFFERENT WAYS OF CALLING A FUNCTION
Depending on whether the function accepts arguments or not and returns a
value or not, there can be four different aspects of C function calls, which are:
• C Programming Functions Without Arguments and Return Value
A function in C programming may not accept an argument and return a value.
Here’s an example of such a function.
#include<stdio.h>
void main (){
printf("Welcome to ");
printName();
}
void printName(){
printf("Simplilearn");
}
• C programming Functions With No Arguments But has a Return Value
A function can return a value without accepting any arguments. Here’s an
example of calculating and returning the area of a rectangle without taking any
argument.
#include<stdio.h>
void main(){
int area = rect_Area();
printf("The area of the Rectangle is: %d\n",area);
}
int rect_Area(){
int len, wid;
printf("Enter the length of the rectangle: ");
scanf("%d",&len);
printf("Enter the width of the rectangle: ");
scanf("%d",&wid);
return len * wid;
}
• C programming Functions With Arguments But No Return Value
C functions may accept arguments but not provide any return value. Given
below is an example of such a function.
#include<stdio.h>
void main(){
int x,y;
printf("Enter the two numbers to add:");
scanf("%d %d",&x,&y);
add(x,y);
}
// Accepting arguments with void return type
void add(int x, int y){
printf("The sum of the numbers is %d",x+y);
}
• C Programming Functions That Accept Arguments and Give a Return Value
Most C functions will accept arguments and provide a return value. The
following program demonstrates a function in C programming that takes
arguments and returns a value.
#include<stdio.h>

void main(){
int x,y,res;
printf("Enter the two numbers to add:");
scanf("%d %d",&x,&y);
res = add(x,y);
printf("The sum of the numbers is %d",res);
}
int add(int x, int y){
return x+y;
}
MORE ABOUT FUNCTIONS IN C PROGRAMMING
• Every program in C has a function. Even if you do not use a library or user-
defined function, you will have to use the main function. The main function is the
program’s entry point, as that is where the compiler will start executing the code.
• Even if a function does not return a value, it has a return type. If a return value is
provided, the return type is the data type of the value. But if there is no return
value, then the void is the return type of the function.
• C functions cannot return array and function types. However, you can easily
overcome this limitation with the use of pointers.
• While in C++, void func() and void func(void) mean the same; it is not the case
with C programming. In C, a function declared without any parameter list can be
called with any number of parameters. Hence, it is advisable to declare a
function as void func(void) and not void func() if you want to call a function
without any parameter.
• If you call a function before the declaration, the C compiler will by default
consider the return type to be int and show an error if the data type of the return
value is anything except int.
KEY FUNCTION IN C PROGRAMMING
You would have noticed that we have declared a function named main() in every example
that we have seen in this article. In fact, not just this article, every C program that you
see will have a main function. The main function in C programming is a special type of
function that serves as the entry point of the program where the execution begins. By
default, the return type of the main function is int. There can be two types of main()
functions: with and without parameters.
Main function without parameters:
int main(){
… //statements
return 0;
}
Main function with parameters:
int main(int argc, char * const argv[]){
… //statements
return 0;
}
Note: We can write a program without the main function, but it is a frowned-upon
practice. It is always best to use the main function for ease of readability and to
understand the program.
RECURSIVE FUNCTIONS IN C PROGRAMMING
Functions in C programming are recursive if they can call themselves until the exit condition is satisfied. If a
function allows you to call itself within the definition in a C program, it is a recursive function. These functions run
by stacking the calls until the exit condition is met and the flow of the program exits the function. Suppose you
want to find the factorial of a number in C; you can use a recursive function to get the result. Here’s an example to
find the factorial of 8.

#include <stdio.h>
int fact(int num);
int main(){
int i = 8;
printf("The factorial of %d is: %d\n", i, fact(i));
return 0;
}
int fact(int num){
if (num == 1)
// Exiting condition
return (1);
else
return (num * fact(num - 1));
}
Let’s calculate the factorial of 8 and confirm if the output is true.
The factorial of the number is calculated as fact(n) = n * (n-1) * (n-
2) * … 1. Hence, the factorial of 8 is 8*7*6*5*4*3*2*1 = 40320. This
confirms that our recursive function worked correctly.
REFERENCE
https://www.simplilearn.com/tutorials/c-tutorial/function-in-c-programming

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy