[go: up one dir, main page]

0% found this document useful (0 votes)
110 views17 pages

Programming Fundamentals: 4.00 Credit Hours, Fall 2015, Graduate Program

This document provides an overview of programming fundamentals including function calls, return values, and recursive functions. 1) Functions can pass arguments by value, where the called function receives copies of the arguments, or by address, where the called function receives the memory addresses and can modify the original arguments. 2) A function can only return a single value. To return multiple values, they must be returned indirectly by passing addresses. 3) Recursive functions call themselves within their own definition. They are used to solve problems by breaking them down into simpler cases, with the base case returning a value and other cases calling the function on simpler inputs.

Uploaded by

Mir Fida Nadeem
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)
110 views17 pages

Programming Fundamentals: 4.00 Credit Hours, Fall 2015, Graduate Program

This document provides an overview of programming fundamentals including function calls, return values, and recursive functions. 1) Functions can pass arguments by value, where the called function receives copies of the arguments, or by address, where the called function receives the memory addresses and can modify the original arguments. 2) A function can only return a single value. To return multiple values, they must be returned indirectly by passing addresses. 3) Recursive functions call themselves within their own definition. They are used to solve problems by breaking them down into simpler cases, with the base case returning a value and other cases calling the function on simpler inputs.

Uploaded by

Mir Fida Nadeem
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/ 17

Course:

Programming Fundamentals
4.00 Credit Hours, Fall 2015,
Graduate Program
Instructor: Maryam Ehsan

1
Today’s lecture outline
• Function call by address or pointer
• Function return value
• Recursive functions

2
Function Calls
• Arguments can generally be passed to
functions in one of the two ways:
– sending the values of the arguments
– sending the addresses of the arguments

3
Example program 1 – function call by value
Memory
main()
a 10 b 20
6504 7505

change_value()

x 20
10 y 40
20
3205 4350

Program Output
a = 10 b = 20
x = 20 y = 40
a = 10 b = 20
Press any key to continue … 4
Example program 1 – function call by address
Memory
change_value(6505, 7505); main()
a 20
10 b 40
20
6504 7505

change_value()

x 6504 y 7505
3205 4350

Program Output
a = 10 b = 20 *x =*(6504) = 10
x = 20 y = 40
a = 20 b = 40
Press any key to continue … 5
Example program 2-function call by value

6
Example program 2-function call by address

7
Return value of a function

8
Points to remember
• Passing arguments by value is not the most
efficient means for programming in C
• When arguments are passed by value, the
called function is unable to modify the original
contents of the incoming parameters
• When arguments are passed by address, the
called function is able to modify the original
contents of the incoming parameters
• A function only return a single value

9
Conclusion
• If we want that the value of an actual argument
should not get changed in the function being
called, pass the actual argument by value.

• If we want that the value of an actual argument


should get changed in the function being called,
pass the actual argument by reference.
• If a function is to be made to return more than
one value at a time then return these values
indirectly by using a call by reference.

10
Recursive function
• In C/C++, it is possible for the functions to call
themselves
• A function is called ‘recursive’ if a statement
within the body of a function calls the same
function

11
Example program - factorial

12
Example program – factorial using
recursion

13
Cont.

14
Cont.
rec ( 3 ) rec ( 2 )
main() { {
{ int f ; int f ;
…. if ( 3 == 1 ) false if ( 2 == 1 ) false
…. return ( 1 ) ; return ( 1 ) ;
fact = rec(3); else else
printf ( "%d ", fact); f = 3 * rec ( 3 - 1 ) ; f = 2 * rec ( 2 - 1 ) ;
} return ( f ) ; return ( f ) ;
} }

rec ( 1 )
f = 3 * 2; f = 2 * 1; {
f=6 f=2 int f ;
if ( 1 == 1 ) true
return ( 1 ) ;
fact = 6
else
f = 2 * rec ( 2 - 1 ) ;
return ( f ) ;
} 15
Example program 2
• Write a definition of a function that adds n
integers using recursion and then return the
sum. Prototype of the function is below
– int sum_number(int);

16
Cont.
rec ( 3 ) rec ( 2 )
main() { {
{ int f ; int f ;
…. if ( 3 == 1 ) false if ( 2 == 1 ) false
…. return ( 1 ) ; return ( 1 ) ;
fact = rec(3); else else
printf ( "%d ", fact); f = 3 + rec ( 3 - 1 ) ; f = 2 + rec ( 2 - 1 ) ;
} return ( f ) ; return ( f ) ;
} }

rec ( 1 )
f = 3 + 3; f = 2 + 1; {
f=6 f=3 int f ;
if ( 1 == 1 ) true
return ( 1 ) ;
fact = 6
else
f = 1 + rec ( 1 - 1 ) ;
return ( f ) ;
} 17

You might also like