0% found this document useful (0 votes)
4 views22 pages

Module 3 Functions

The document provides an overview of functions in programming, explaining their definition, advantages, and types, including predefined and user-defined functions. It details how to write, declare, call, and define functions, along with examples demonstrating various functionalities such as returning values and using parameters. Additionally, it distinguishes between actual and formal parameters, and includes homework questions for practice.

Uploaded by

o7athulkrishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Module 3 Functions

The document provides an overview of functions in programming, explaining their definition, advantages, and types, including predefined and user-defined functions. It details how to write, declare, call, and define functions, along with examples demonstrating various functionalities such as returning values and using parameters. Additionally, it distinguishes between actual and formal parameters, and includes homework questions for practice.

Uploaded by

o7athulkrishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

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

Advantages of Using Functions


• Code reusability
• Better readability
• Easy debugging and maintenance
• Modular programming
Types of function
1.Predefined 2.User defined functions
Functions/Library functions This type of functions has to be
2.User defined functions developed by the user at the time
1.Predefined of writing a program.
Functions/Library functions Example:
• These functions are pre- Main() is an example of user
defined. So, we do not need to defined function
write library functions ourselves.
• Examples: printf(),
scanf(),sqrt(),strcat() etc
How to write a function?
• A function has 3 parts
1. Function declaration
2. Function call
3. Function definition
Function Declaration(prototype)
return_type function_name(parameter_list);
• Write Before main() function

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

• Information can be passed to functions as a parameter.


Parameters act as variables inside the function.
• Parameters are specified after the function name, inside
the parentheses. You can add as many parameters as
you want, just separate them with a comma:
Syntax
returnType functionName(parameter1, parameter2,
parameter3) {
// code to be executed
}
Example3- sum of 2 numbers(Function Without Return
Value but With 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.

You might also like