0% found this document useful (0 votes)
3 views41 pages

Unit No 05 New

Uploaded by

yuvraj
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)
3 views41 pages

Unit No 05 New

Uploaded by

yuvraj
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/ 41

Unit No : 05 User

Defined function
Syllabus :
• User Defined Functions: Need for User-defined Functions, A Multi-Function
Program, Elements of User defined Functions, Definition of Functions, Return
Values and their Types, Function Calls, Function Declaration, Category of
Functions: No Arguments and no Return Values, Arguments but No Return
Values, Arguments with Return values, No Arguments but Returns a Value,
Functions that Return Multiple Values, Nesting of Functions, Recursion
• Structures : What is a Structure? Structure Type Declarations, Structure
Declarations, Referencing Structure Members, Referencing Whole Structures,
Initialization of Structures.
Function in c
• Function is basically a set of statements that takes inputs, perform
some computation and produce output.

• Syntax:
Library Function :
Need for User-defined Functions :

• 1. Code reusability .
• 2.Modularity.
• 3.Error detection.
• 4.Flexibility.
A Multi-Function Program
• Program Explanation: in the program given above we have declared and
defined 3 functions happyNewYear(), happyDiwali() and friendShipDay()
respectively.
after that in the main() function we have called the declared and defined function
happyNewYear(); ,happyDiwali(); ,friendShipDay() one by one respectively.
Upon successful execution and completion of the program the called function
gives the
Happy new Year! Happy Diwali! Happy friendship Day! Result

• Output:
Happy new Year!
Happy Diwali!
Happy friendship Day!
C program to demonstrate multiple function inside the main() function.
#include<stdio.h>
void happyDiwali()
{
printf("\n Happy Diwali!");
}
void friendShipDay()
{
printf("\n Happy friendship Day!");
}
void happyNewYear()
{
printf("\n Happy new Year!");
}
int main()
{
// calling function
happyNewYear();
happyDiwali();
friendShipDay();
return(0);
}
Definition of Functions
Syntax :
Function Calls
Function Calls :
Function Declaration
Return values and their types
Return values and their types
• Here are the three primary forms of the return statement in C programming:
• 1. Return with an Expression:
• return expression;
• In this form, the expression represents the value that the C function returns to the calling code. This value
could be a constant, a variable, or the result of a computation.
• 2. Return with a Value:
• return;
• When a function has no return value or is used to perform a task without providing any output, the return
statement can be used without an expression. This form simply indicates the end of the function's execution.
Return values and their types
• 3. No Return Statement:
• void myFunction() {
• // Function logic without a return statement
•}
• If a function does not require a return statement, it can be defined
with a return type of void, indicating that the function does not return
any value
Category of Functions:

1) No Arguments and no Return Values .


2) Arguments but No Return Values.
3) Arguments with Return values.
4) No Arguments but Returns a Value.
5) Functions that Return Multiple Values.
1) No Arguments and no Return Values .

•e
Sample program :
2) No Arguments but Returns a Value.
Sample program :
3) Arguments but No Return Values
Sample program :
4) Arguments with Return values.
Sample program :
Functions that Return Multiple
Values
Nesting of Functions
• Nested function is not supported by C because we
cannot define a function within another function in C.
We can declare a function inside a function, but it’s not
a nested function.
Because nested functions definitions can not
access local variables of the surrounding blocks, they
can access only global variables of the containing
module. This is done so that lookup of global variables
doesn’t have to go through the directory. As in C, there
are two nested scopes: local and global (and beyond
this, built-ins). Therefore, nested functions have only a
limited use. If we try to approach nested function in C,
then we will get compile time error.
// C program to illustrate the
// concept of Nested function.
Output:
#include <stdio.h> Compile time error: undefined reference to `view'
int main(void) An extension of the GNU C Compiler allows the declarations of nested functions.
{ The declarations of nested functions under GCC’s extension need to be
prefix/start with the auto keyword.
printf("Main");
int fun()
{
printf("fun");

// defining view() function inside fun()


function.
int view()
{
printf("view");
}
return 1;
}
view();
}
Recursion
• Recursion is the technique of making a function call itself.
Example :
Structures :
• What is a Structure?.
C Structure Declaration
• We have to declare structure in C before using it in our program. In structure
declaration, we specify its member variables along with their datatype. We can
use the struct keyword to declare the structure in C using the following syntax:

• syntax:
• struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
C Structure Definition
To use structure in our program, we have to define its instance. We
can do that by creating variables of the structure type.
We can define structure variables using two methods:
1. Structure Variable Declaration with Structure Template
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;

2. Structure Variable Declaration after Structure Template


// structure declared beforehand
struct structure_name variable1, variable2, .......;
Access Structure Members

We can access structure members by


using the ( . ) dot operator.
Syntax
structure_name.member1;
strcuture_name.member2;
Initialize Structure Members
Structure members cannot be initialized with the declaration. For example, the following C program fails in
the compilation.
struct Point
{
int x = 0; // COMPILER ERROR: cannot initialize members here
int y = 0; // COMPILER ERROR: cannot initialize members here
};
The reason for the above error is simple. When a datatype is declared, no memory is allocated for it. Memory
is allocated only when variables are created.
Default Initialization
By default, structure members are not automatically initialized to 0 or NULL. Uninitialized structure members
will contain garbage values. However,
when a structure variable is declared with an initializer, all members not explicitly initialized are zero-
initialized.
struct Point
{
int x;
int y;
};

struct Point p = {0}; // Both x and y are initialized to 0


We can initialize structure members in 3 ways which are as follows:
Using Assignment Operator.
1.
Using Initializer List.
2.
Using Designated Initializer List.
3.

1. Initialization using Assignment Operator


struct structure_name str;
str.member1 = value1;
str.member2 = value2;
str.member3 = value3;
.

2. Initialization using Initializer List

struct structure_name str = { value1, value2, value3 };


In this type of initialization, the values are assigned in sequential order as they are declared in
the structure template.
3. Initialization using Designated Initializer List
Designated Initialization allows structure members to be initialized in any order. This feature has
been added in the C99 standard.
struct structure_name str = { .member1 = value1, .member2 = value2, .member3 =
value3 };
The Designated Initialization is only supported in C but not in C++.
Break and continue statement :

You might also like