Docsity

Short notes of Functions in C programming, Summaries of C programming

Toggle sidebar
bg1
UNIT-III
Functions
A function in C is a set of statements that when called perform some specific
task. It is the basic building block of a C program that provides modularity
and code reusability.
Syntax of Functions in C
The syntax of function can be divided into 3 aspects:
1. Function Declaration
2. Function Definition
3. Function Calls
Function Declarations
In a function declaration, we must provide the function name, its return type,
and the number and type of its parameters. A function declaration tells the
compiler that there is a function with the given name defined somewhere else
in the program.
Syntax
return_type name_of_the_function (parameter_1, parameter_2);
Example
int sum(int a, int b);
int sum(int , int);
***A function in C must always be declared globally before calling it.
Function Definition
The function definition consists of actual statements which are executed when
the function is called
A C function is generally defined and declared in a single step because the
function definition always starts with the function declaration so we do not
need to declare it explicitly.
Syntax
return_type function_name (para1_type para1_name, para2_type
para2_name)
{
// body of the function
}
pf3
pf4
pf5

Partial preview of the text

Download Short notes of Functions in C programming and more Summaries C programming in PDF only on Docsity!

UNIT-III

Functions

A function in C is a set of statements that when called perform some specific task. It is the basic building block of a C program that provides modularity and code reusability. Syntax of Functions in C The syntax of function can be divided into 3 aspects:

  1. Function Declaration
  2. Function Definition
  3. Function Calls Function Declarations In a function declaration, we must provide the function name, its return type, and the number and type of its parameters. A function declaration tells the compiler that there is a function with the given name defined somewhere else in the program. Syntax return_type name_of_the_function ( parameter_1 , parameter_2 ); Example int sum(int a , int b ); int sum(int , int); ***A function in C must always be declared globally before calling it. Function Definition The function definition consists of actual statements which are executed when the function is called A C function is generally defined and declared in a single step because the function definition always starts with the function declaration so we do not need to declare it explicitly. Syntax return_type function_name (para1_type para1_name, para2_type para2_name ) { // body of the function }

Function Call A function call is a statement that instructs the compiler to execute the function. We use the function name and parameters in the function call. Example #include // Function that takes two parameters // a and b as inputs and returns // their sum int sum(int a, int b) { return a + b; } int main() { // Calling sum function and // storing its value in add variable int add = sum(10, 30); printf("Sum is: %d", add); return 0; } Function Return Type Function return type tells what type of value is returned after all function is executed. When we don’t want to return a value, we can use the void data type. Example: int func(parameter_1,parameter_2); Function Arguments Function Arguments (also known as Function Parameters) are the data that is passed to a function. Example: int function_name(int var1, int var2);

2. User Defined Function Functions that the user creates are known as User-Defined functions. User- defined functions can be improved and modified according to the need of the programmer. Advantages of User-Defined Functions

  • Changeable functions can be modified as per need.
  • The Code of these functions is reusable in other programs.
  • These functions are easy to understand, debug and maintain. Example #include int sum(int a, int b) { return a + b; } int main() { int a = 30, b = 40; // function call int res = sum(a, b); printf("Sum is: %d", res); return 0; } Passing Parameters to Functions The data passed when the function is being invoked is known as the Actual parameters. Formal Parameters are the variable and the data type as mentioned in the function declaration. We can pass arguments to the C function in two ways:
  1. Pass by Value
  2. Pass by Reference
  3. Pass by Value Parameter passing in this method copies values from actual parameters into formal function parameters. As a result, any changes made inside the functions do not reflect in the caller’s parameters.

#include void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; } int main() { int var1 = 3, var2 = 2; printf("Before swap Value of var1 and var2 is: %d, %d\n", var1, var2); swap(var1, var2); printf("After swap Value of var1 and var2 is: %d, %d", var1, var2); return 0; }

2. Pass by Reference The caller’s actual parameters and the function’s actual parameters refer to the same locations, so any changes made inside the function are reflected in the caller’s actual parameters. #include void swap(int *var1, int *var2) { int temp = *var1; *var1 = *var2; *var2 = temp; } int main() { int var1 = 3, var2 = 2; printf("Before swap Value of var1 and var2 is: %d, %d\n", var1, var2); swap(&var1, &var2); printf("After swap Value of var1 and var2 is: %d, %d", var1, var2); return 0; }