Module 3 Functions
Module 3 Functions
Prepared By,
Anugraha K.R
Function
• A function is a block of code which only runs when it is
called.
• 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.
Advantages of Using Functions
Example:
Int add(int, int); // Function declaration with return type int
Void add(int,int);
Function Definition
return_type function_name(parameter_list) • Example
{ int add(int a, int b)
// Function body {
return value; return a + b;
} }
Function Call
• function_name(arguments);
example:
result = add(5, 3);
Example1: Function Without Return
Value and Without Arguments
#include <stdio.h> Here function is defined before main(), so
void myFunction() no need of function declaration
{
printf("welcome!"); Output
welcome!
}
void main()
{
myFunction();
}
Predict the output
#include <stdio.h>
void myFunction()
{
printf(“I like programming!\n");
}
Void main() {
myFunction(); // call the function
myFunction(); // call the function
myFunction(); // call the function
}
Output
I like programming!
I like programming!
I like programming!
Example 2: Calculate the Sum of Numbers
using function
#include <stdio.h> Output
void calculateSum()
{
The sum of x + y is : 15
int x = 5;
int y = 10;
int sum = x + y;
printf("The sum of x + y is: %d", sum);
}
void main()
{
calculateSum(); // call the function
}
Parameters and Arguments
#include <stdio.h>
void calculateSum(int , int );
void main() Output
The sum of 5 + 3 is: 8
{ The sum of 8 + 2 is: 10
calculateSum(5, 3); The sum of 15 + 15 is: 30
calculateSum(8, 2);
calculateSum(15, 15);
}
void calculateSum(int x, int y) {
int sum = x + y;
printf("The sum of %d + %d is: %d\n", x, y, sum);
}
Function With Return Value but
Without Arguments
#include <stdio.h> int sum()
int sum(); {
void main() int a, b,sum;
{ printf("Enter two numbers: ");
int result = sum(); // Function call scanf("%d %d", &a, &b);
printf("Sum of the two numbers: sum=a+b;
%d\n", result); return sum; // Returning the
} sum Output
Enter two numbers: 2 3
} Sum of the two numbers: 5
Function With Return Value and With
Arguments
#include <stdio.h> int sum(int x,int y)
int sum(int,int); {
void main()
int sum;
{
int a,b; sum=x+y;
printf("enter two numbers"); return sum; // Returning the
scanf("%d%d",&a,&b); sum
enter two numbers3 4
int result = sum(a,b); // Function call } Sum of the two numbers: 7
printf("Sum of the two numbers: %d\n",
result);
}
Array as parameter
#include <stdio.h> void main()
int sumArray(int arr[], int size) {
{ int arr[] = {1, 2, 3, 4, 5};
int sum = 0;
int size = sizeof(arr) /
for (int i = 0; i < size; i++) sizeof(arr[0]);
{
int result = sumArray(arr, size);
sum =sum+ arr[i];
printf("Sum of array elements:
}
%d\n", result);
return sum;
}
}
Homework Questions:
1. Write a function without return value and without arguments to
print "Hello, World!" 10 times.
2. Write a function that takes two numbers as arguments and prints
their sum. The function should not return any value.
3. Write a function that reads a number from the user and returns its
square.(function with argument and return type).
Actual and formal parameters of a
function
Parameters are variables that are used to pass on values or references
between functions. There are two kinds of parameters:
1.actual parameters
2.formal parameters.
Actual parameters
Actual parameters are those values that you pass to a function when it
is called.It is also called arguments.
Example- actual parameters
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
Void main()
{
int result = add(10, 5); // 10 and 5 are actual parameters
printf("The sum is: %dn", result);
}
Formal parameters
• Formal parameters are variables defined in the function header that
receive the values of the actual parameters given during function
calls. It is also called function parameters.
• If actual parameters are the inputs, formal parameters are the hands
ready to catch them.
example
#include <stdio.h>
int add(int a, int b)
{
return a + b;
}
in this function definition, a and b are formal parameters. They
substitute whatever actual parameters we provide when we call the
function.