0% found this document useful (0 votes)
4 views

Week 12 - Functions

The document explains user-defined functions and recursive functions in programming, detailing their purpose, structure, and types. It covers function prototypes, definitions, calls, and the distinction between formal and actual arguments, as well as examples of different function types. Additionally, it introduces recursive functions, their components, and provides examples of recursion in computing tasks such as calculating factorials and sums of natural numbers.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week 12 - Functions

The document explains user-defined functions and recursive functions in programming, detailing their purpose, structure, and types. It covers function prototypes, definitions, calls, and the distinction between formal and actual arguments, as well as examples of different function types. Additionally, it introduces recursive functions, their components, and provides examples of recursion in computing tasks such as calculating factorials and sums of natural numbers.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Week 12 – User Defined Functions and Recursive Functions

FUNCTIONS-PURPOSE

• Functions break large computing tasks into smaller ones.

• Avoids repetition of Codes.

A function is a set of statements that take inputs, do some specific computation and produces
output.

Elements of the functions

Function prototype / function declaration

Function Call Function definition

Formal Arguments

Return Value

Actual Arguments

Function name

Function prototype / function declaration

Syntax :

return_type function_name(arguments);

Eg: int add(int, int);


Function definition

Syntax :

return_type function_name(formal arguments)

----- function body ----

Eg: int add(int x, int y)

------

return c;

Function call

Syntax:

function_name(Actual Arguments);

Eg : int d=add(a,b);

Actual arguments

The arguments(values passed)used in the function call

Formal arguments

The arguments mentioned in the function definition

Return Value

return statement used to return the values back to the calling function.

/* Funtion to add two numbers */

#include<stdio.h>

int main()

int a,b,d;

void add(int,int); /* function declaration */

printf("Enter a,b:");
scanf("%d%d",&a,&b);

d= add(a,b); /* function call , a, b – actual arguments */

printf(“%d”,d);

int add(int x,int y) /* function definition x, y – formal arguments */

int c;

c=x+y;

return c;

Four types of function prototypes

1. Function with no argument and no return value

2. Function with no argument and with return value

3. Function with argument and no return value

4. Function with argument and with return value

Function with no argument and no return value

• In this prototype, no data transfer takes place between the calling function and the called

function. (ie from function call to function definition).

• The function is only executed, does not return any value to the calling program.

//Program for no argument and no return value


#include<stdio.h>

void add(); // function declaration

main()

add(); // function call

void add() // function definition

int a,b,c;

a=10,b=20;

c=a+b;

printf(“sum is %d”,c);

Output:- sum is 30

Function with no argument and with return value

• 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.

//Program for no argument and with return value

#include<stdio.h>

int add(); // function declaration

int main()

int sum;
sum=add(); // function call

printf(“sum is %d”,sum);

int add() // function definition

int a,b,c;

a=10,b=20;

c=a+b;

return(c);

Output:- sum is 30

Function with argument and no return value

• 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.

//Program for argument and no return value

#include<stdio.h>

void add(int,int); // function declaration

int main()

int a=10,b=20;

add(a,b); // function call

}
void add(int a,int b) // function definition

int c;

c=a+b;

printf(“sum is %d”,c);

Output:- sum is 30

Function with argument and with return value

• 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.

//Program for argument and with return value

#include<stdio.h>

int add(int,int); // function declaration

int main()

int a=10,b=20,c;

c=add(a,b); // function call

printf(“sum is %d”,c);

int add(int a,int b) // function definition

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.

PARTS OF RECURSIVE FUNCTIONS

BASE PART

Base Condition :to be mentioned for the termination of the recursive call

RECURSIVE PART :

Recursive call: invocation of the recursive call

TYPES OF RECURSIVE FUNCTIONS

DIRECT - Same function calling itself again and again

INDIRECT - function1 invokes another function2, and function2 invokes function1

EXAMPLE 1
Find the factorial of a given number.

Base condition:

0!=1 and 1!=1,we will consider this to derive the base

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.

You might also like