Module 1 Functions
Module 1 Functions
Calling function
Called function
Functions
printf() sum()
scanf() sub()
… …
Function Declaration/Prototype
Function Definition
Function Call
int sum(int,int);
• If function declaration is global,
example void sum (int,int); then argument names are
void sum(); optional in declaration
int sum();
int sum(int x,int y);
syntax
{
statements;
… Function body
return variable; // if return_data_type is not void
}
example
int sum(int x,int y) • Number of arguments and type of arguments in
{ function header and declaration should match
• Function definition itself can act as implicit
} function declaration(the function declaration
can be skipped & function should be defined
A function can only return one value
before used)
• Invokes a function
• Executes the statements in called function, returns control back to calling function
actual arguments
example sum(a,b);
#include<stdio.h> #include<stdio.h>
#include<stdio.h>
int multiply(int a, int b); // function declaration
int main()
{
int i, j, result;
printf("Enter 2 numbers to multiply...");
scanf("%d%d", &i, &j);
result = multiply(i, j); // function call
printf("The result of multiplication is: %d", result);
return 0;
}
int multiply(int a, int b) // function defintion
{
return (a*b);
}
Function Declaration
A function declaration is also known as the function prototype and it
consists of 4 parts given below.
• Return type
• Name of the function
• List of parameters
• Terminating semicolon
int sum(int a,int b);
Function definition
RETURN STATEMENT
• Used to terminate the execution of a function and returns the control to the calling function
• If a function do not have a return statement, the control automatically returns to the calling function
after the last statement of the called function is executed
return expression;
(or)
return (expression);
}
JEYAVIM SHERIN, SCOPE, VIT-CHENNAI
FUNCTIONS-Armstrong/Perfect number (contd.)
void checkperfect(int n1)
{
int i, sum, num;
sum = 0;
num = n1;
for(i=1; i<num; i++)
{
/* If i is a divisor of n1 */
if(num%i == 0)
{
sum += i;
}
}
if(n1 == sum)
printf(" The %d is a Perfect number.\n", n1);
else
printf(" The %d is not a Perfect number.\n", n1);
}
JEYAVIM SHERIN, SCOPE, VIT-CHENNAI
FUNCTIONS-palindrome
Function definition before main() function
#include <stdio.h>
int ispalindrome(int num) { int main() {
int original = num; int number;
int reversed = 0, digit; printf("Enter a number: ");
while (num != 0) { scanf("%d", &number);
digit = num % 10; if (ispalindrome(number))
reversed = reversed * 10 + digit; printf("%d is a Palindrome number.\n", number);
num = num / 10; else
} printf("%d is Not a Palindrome number.\n", number);
if (original == reversed) return 0;
return 1; }
else
return 0;
}
#include <stdio.h>
#include <stdio.h> Value in calling
int add(int n);
void add(int n); function changes if we
int main()
int main() return the value
{
{
int n = 10;
int n = 10;
n=add(n);
add(n);
printf(“calling function:%d”,n);
printf(“calling function:%d”,n);
return 0;
return 0;
}
}
int add(int n)
void add(int n)
{
{
n=n+10;
n=n+10;
printf(“called function:%d",n);
printf(“called function:%d",n); Drawback:
return n;
} • Consumes
}
Output: additional storage
Output:
called function:20 space
called function:20
calling function:10 • Time required for
calling function:20
copy
#include <stdio.h>
#include <stdio.h>
void swap(int*, int*);
void swap(int x, int y);
int main()
int main()
{
{
int a = 10, b = 20;
int a = 10, b = 20;
swap(&a, &b);
swap(a, b);
printf("a=%d b=%d\n", a, b);
printf("a=%d b=%d\n", a, b);
return 0;
return 0;
}
}
void swap(int* x, int* y)
void swap(int x, int y)
{
{
int t;
int t;
t = *x;
t = x;
*x = *y;
x = y;
*y = t;
y = t;
printf("x=%d y=%d\n", *x, *y);
printf("x=%d y=%d\n", x, y);
}
}
Output: x=20 y=10 a=10 b=20
Output: x=20 y=10 a=20 b=10
JEYAVIM SHERIN, SCOPE, VIT-CHENNAI
FUNCTIONS & ARRAYS
Function call No []
function_name(array_name);
Function declaration
return_type function_name(data_type array_name[]);
Advantages:
Disadvantages :
• It is slower than non recursive programs due to the overhead of maintaining the stack.
• It requires more memory for the stack.
• For better performance, use loops instead of recursion. Because recursion is slower.
JEYAVIM SHERIN, SCOPE, VIT-CHENNAI
RECURSION
• Divide a problem into sub problems
• Solve the subproblems
• Combine the results
return_type function_name(arguments)
{
if(base condition)
return ;
else
return function_name(arguments)
if(n==0 || n==1)
fact(4) return 1;
else
return 4*fact(3);
if(n==0 || n==1)
fact(3) return 1;
else
return 3*fact(2);
if(n==0 || n==1)
return 1;
fact(2) else
return 2*fact(1);
}
}
int main()
{
int n;
scanf("%d",&n);
show(n);
return 0;
} JEYAVIM SHERIN, SCOPE, VIT-CHENNAI
RECURSION(Fibonacci Series)
#include<stdio.h>
int fib(int);
int main ()
{
int num,fibo;
scanf(“%d”,&num);
fibo = fib(num);
printf("Result is: %d",fibo);
}
int fib(int num)
{
if (num==0||num==1)
{
return num;
}
else
{
return fib(num-1)+fib(num-2);
}
} JEYAVIM SHERIN, SCOPE, VIT-CHENNAI
1. Write a C program using a function to print your name and roll number.
2. Write a function that accepts two integers and prints their sum.
3. Write a program using a function to find the square of a number.
4. Write a function to check whether a number is even or odd.
5. Write a C program using a function to find the maximum of two numbers.
6. Write a function that returns the sum of two numbers and display the result in
main().
7. Write a function to return the factorial of a number.
8. Write a function to return the reverse of a given number.
9. Write a function that returns the sum of digits of a number.
[Link] a function to check whether a number is prime or not.