Week 12 - Functions
Week 12 - Functions
FUNCTIONS-PURPOSE
A function is a set of statements that take inputs, do some specific computation and produces
output.
Formal Arguments
Return Value
Actual Arguments
Function name
Syntax :
return_type function_name(arguments);
Syntax :
------
return c;
Function call
Syntax:
function_name(Actual Arguments);
Eg : int d=add(a,b);
Actual arguments
Formal arguments
Return Value
return statement used to return the values back to the calling function.
#include<stdio.h>
int main()
int a,b,d;
printf("Enter a,b:");
scanf("%d%d",&a,&b);
printf(“%d”,d);
int c;
c=x+y;
return c;
• In this prototype, no data transfer takes place between the calling function and the called
• The function is only executed, does not return any value to the calling program.
main()
int a,b,c;
a=10,b=20;
c=a+b;
printf(“sum is %d”,c);
Output:- sum is 30
• In this prototype, the calling program cannot pass any arguments to the called program. But the
called program may send some value return to the calling program.
#include<stdio.h>
int main()
int sum;
sum=add(); // function call
printf(“sum is %d”,sum);
int a,b,c;
a=10,b=20;
c=a+b;
return(c);
Output:- sum is 30
• In this prototype, data is transferred from calling function to called function. i.e called function
receives some data from the calling function and does not return any values to the calling function.
#include<stdio.h>
int main()
int a=10,b=20;
}
void add(int a,int b) // function definition
int c;
c=a+b;
printf(“sum is %d”,c);
Output:- sum is 30
• In this prototype, the data is transferred between the calling function and called function. i.e the
called function receives some data from the calling function and return a value to the calling
function.
#include<stdio.h>
int main()
int a=10,b=20,c;
printf(“sum is %d”,c);
int c;
c=a+b;
return(c);
Output:- sum is 30
Ans: A
Ans: A
Ans: A
Ans: B
Ans: B
Ans: B
Ans: A
Ans: C
Determine the factors of a number (i.e., all positive integer values that evenly divide into a number)
and then return the pth element of the list, sorted ascending. If there is no pth element, return 0.
Given an integer, if the number is prime, return 1. Otherwise return its smallest divisor greater than
1.
A binary number is a combination of 1s and 0s. Its nth least significant digit is the nth digit starting
from the right starting with 1. Given a decimal number, convert it to binary and determine the value
of the 4th least significant digit.
Recursive Functions
A function which call itself again and again is termed as recursive function.
There are certain problems which are recursive in nature. Such problems can be solved using
recursive function.
When problem is solved using recursion the source code looks elegant.
BASE PART
Base Condition :to be mentioned for the termination of the recursive call
RECURSIVE PART :
EXAMPLE 1
Find the factorial of a given number.
Base condition:
condition.
if(n==0)||(n==1) return
Recursive Call:
fact(n)=n*fact(n-1)
1
Sum of Natural Numbers Using Recursion
Ans: D
Ans: D
Ans: B
Ans: C
Ans: B
Ans: C
You are a bank account hacker. Initially you have 1 rupee in your account, and you want exactly N
rupees in your account. You wrote two hacks, first hack can multiply the amount of money you own
by 10, while the second can multiply it by 20. These hacks can be used any number of time. Can you
achieve the desired amount N using these hacks
You are given two numbers n and k. For each number in the interval [1, n], your task is to calculate its
largest divisor that is not divisible by k. Print the sum of these divisors.