4 - Functions and Structure
4 - Functions and Structure
#include <iostream>
using namespace std;
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int result;
return 0;
}
When the above code is compiled and executed, it produces the following result:
1
C Programming Recursion / Recursive Functions
Example of recursion in C programming Write a C program to find sum of first n natural numbers using
recursion. Note: Positive integers are known as natural number i.e. 1, 2, 3....n
#include <iostream>
using namespace std;
int sum(int n);
int main()
{
int num,add;
cout<<"Enter a positive integer:\n";
cin>>num;
add=sum(num);
cout<<"sum = "<<add;
return 0;
}
int sum(int n)
{
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}
Output Enter a positive integer:
5
15
sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15
Every recursive function must be provided with a way to end the recursion. In this example when, n is equal
to 0, there is no recursive call and recursion ends.
2
Advantages and Disadvantages of Recursion
Recursion is more elegant and requires few variables which make program clean. Recursion can be used to
replace complex nesting code by dividing the problem into same problem of its sub-type.
In other hand, it is hard to think the logic of a recursive function. It is also difficult to debug the code
containing recursion.
NOTE- This is just a suggestion to compiler to make the function inline, if function is big (in term of executable
instruction etc) then, compiler can ignore the “inline” request and treat the function as normal function.
Example –
#include <iostream>
using namespace std;
3
When the above code is compiled and executed, it produces the following result:
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
Why to use –
In many places we create the functions for small work/functionality which contain simple and less number
of executable instruction. Imagine their calling overhead each time they are being called by callers.
When a normal function call instruction is encountered, the program stores the memory address of the
instructions immediately following the function call statement, loads the function being called into the
memory, copies argument values, jumps to the memory location of the called function, executes the function
codes, stores the return value of the function, and then jumps back to the address of the instruction that was
saved just before executing the called function. Too much run time overhead.
The C++ inline function provides an alternative. With inline keyword, the compiler replaces the function call
statement with the function code itself (process called expansion) and then compiles the entire code. Thus,
with inline functions, the compiler does not have to jump to another location to execute the function, and
then jump back as the code of the called function is already available to the calling program.
With below pros, cons and performance analysis, you will be able to understand the “why” for inline keyword
Pros :-
1. It speeds up your program by avoiding function calling overhead.
2. It save overhead of variables push/pop on the stack, when function calling happens.
3. It save overhead of return call from a function.
4. It increases locality of reference by utilizing instruction cache.
5. By marking it as inline, you can put a function definition in a header file.
Cons :-
1. It increases the executable size due to code expansion.
2. C++ inlining is resolved at compile time. Which means if you change the code of the inlined function,
you would need to recompile all the code using it to make sure it will be updated.
3. When used in a header, it makes your header file larger with information which users don’t care.
4. As mentioned above it increases the executable size, which may cause thrashing in memory. More
number of page fault bringing down your program performance.
5. Sometimes not useful for example in embedded system where large executable size is not preferred
at all due to memory constraints.
4
Structure - C Program to Store Information of Students Using Structure
#include<iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
int i, n=3;
struct student s[3];
cout<<"Enter information of students:\n";
for(i=0;i<n;++i)
{
s[i].roll=i+1;
cout<<"\nFor roll number "<<s[i].roll<<endl;
cout<<"Enter name: ";
cin>>s[i].name;
cout<<"Enter marks: ";
cin>>s[i].marks;
cout<<endl;
}
cout<<"Displaying information of students:\n\n";
for(i=0;i<n;++i)
{
cout<<"\nInformation for roll number "<<i+1<<endl;
cout<<"Name: ";
cout<<s[i].name<<endl;
cout<<"Marks: "<<s[i].marks;
}
return 0;
}