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

4 - Functions and Structure

The document discusses C structures and provides an example program to store student information like name, roll number, and marks using a structure. The program defines a student structure with name, roll, and marks members. It initializes an array of 3 student structures and takes input from the user to fill the structure members. Finally, it displays the stored information by iterating through the structure array.

Uploaded by

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

4 - Functions and Structure

The document discusses C structures and provides an example program to store student information like name, roll number, and marks using a structure. The program defines a student structure with name, roll, and marks members. It initializes an array of 3 student structures and takes input from the user to fill the structure members. Finally, it displays the stored information by iterating through the structure array.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Functions - Default Values for Parameters

#include <iostream>
using namespace std;

int sum(int a, int b=20)


{
int result;
result = a + b;
return (result);
}

int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int result;

// calling a function to add the values.


result = sum(a, b);
cout << "Total value is :" << result << endl;

// calling a function again as follows.


result = sum(a);
cout << "Total value is :" << result << endl;

return 0;
}

When the above code is compiled and executed, it produces the following result:

Total value is :300


Total value is :120

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

For better visualization of recursion in this example:

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.

What is C++ inline functions


Inline function is introduced which is an optimization technique used by the compilers especially to reduce
the execution time. We will cover “what, why, when & how” of inline functions.

What is inline function :


The inline functions are a C++ enhancement feature to increase the execution time of a program. Functions
can be instructed to compiler to make them inline so that compiler can replace those function definition
wherever those are being called. Compiler replaces the definition of inline functions at compile time instead
of referring function definition at runtime.

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.

How to make function inline:


To make any function as inline, start its definitions with the keyword “inline”.

Example –
#include <iostream>
using namespace std;

inline int Max(int x, int y)


{
return (x > y)? x : y;
}

// Main function for the program


int main( )
{

cout << "Max (20,10): " << Max(20,10) << endl;


cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}

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;
}

You might also like