Functions • A functionis a named block of code that performs a specific task. • It allows you to write a piece of logic once and reuse it wherever needed in the program. • This helps keep your code clean, organized, and easier to understand. • A function is a set of statements enclosed within curly brackets ({})
3.
Why Do WeNeed Functions in C Programming? 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
4.
Basic Syntax ofFunctions • The basic syntax of functions in C programming is: return_type function_name(arg1, arg2, … argn) { Body of the function //Statements to be processed }
5.
Aspects of Functionsin C Programming 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.
6.
Types of Functionsin C Functions in C programming are classified into two types: 1. Library Functions 2. User-Defined Functions
7.
Library Functions Also referredto 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.
8.
User-Defined Functions • Theseare 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.
9.
Different Ways ofCalling 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:
10.
Function Prototype • Itspecifies the type of value that the function will return and the types of values that will be passed to it. • It is defined at the beginning, before the function is called. Syntax: return-type function-name(list of arguments); Example: void sum(int, int);
11.
Function Call • Afunction can be called by specifying its name and a list of arguments enclosed in parentheses and separated by commas. • If there are no arguments, empty parentheses are placed after the function name. • If the function returns a value, the function call is written as an assignment statement as: A = sum(x, y);
12.
Function Arguments andParameters • Arguments are also called actual parameters. Arguments are written within parenthesis at the time of function call. • Parameters are also called formal parameters. These are written within parenthesis at the time of function definition.
13.
Return Statement • Itis the last statement of the function that returns certain values. • It returns specific types of values to the place from where the function was called. Syntax: return(variable-name or constant);
14.
Categories of Function •Function with no arguments and no return • Function with arguments but no return • Function with no arguments and return • Function with arguments and return
Call by value •This method copies the value of actual parameters into the formal parameters. • During execution, any changes made to the formal parameters do not affect the actual parameters.
Call by Reference •The reference (address) of the original variable is passed. • The function does not create its own copy; it refers to the original values by reference. • Functions work with the original data, so any changes affect the original data.
#include <stdio.h> // Functiondeclaration int add(int a, int b); int main() { int result = add(5, 10); // Function call printf("Sum: %dn", result); return 0; } // Function definition int add(int a, int b) { return a + b; }