0% found this document useful (0 votes)
27 views12 pages

Programming For Problem Solving Module-3

This document provides an overview of structures and functions in C programming, detailing how to define and use structures to store different data types. It explains the types of functions, including library and user-defined functions, and their classifications based on arguments and return values. Additionally, it covers parameter passing techniques, recursion, and provides example programs for better understanding.

Uploaded by

rishusingh3638
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

Programming For Problem Solving Module-3

This document provides an overview of structures and functions in C programming, detailing how to define and use structures to store different data types. It explains the types of functions, including library and user-defined functions, and their classifications based on arguments and return values. Additionally, it covers parameter passing techniques, recursion, and provides example programs for better understanding.

Uploaded by

rishusingh3638
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Unit-3 Function

Designing a Structure Program


What is a Structure?

A structure is a user-defined data type in C that allows us to store different types of data together under
one name.

Steps for Designing a Structure Program

1. Define the structure

2. Declare structure variables

3. Access structure members

4. Write main program logic

Syntax of Structure

struct structure_name

data_type member1;

data_type member2;

...

};

Example Program: Student Information (Structure Program)

#include <stdio.h>

struct Student

int roll;

char name[20];

float marks;

};

int main()

{
struct Student s;

printf("Enter Roll Number: ");

scanf("%d", &[Link]);

printf("Enter Name: ");

scanf("%s", [Link]);

printf("Enter Marks: ");

scanf("%f", &[Link]);

printf("\n--- Student Details ---\n");

printf("Roll Number: %d\n", [Link]);

printf("Name: %s\n", [Link]);

printf("Marks: %.2f\n", [Link]);

return 0;

Output (Example)

Enter Roll Number: 1

Enter Name: Ram

Enter Marks: 85

--- Student Details ---

Roll Number: 1

Name: Ram

Marks: 85.00
Types of Functions
[Link] Functions
Library functions are predefined functions provided by C language and stored in header files.

Examples

• printf()

• scanf()

• strlen()

• sqrt()

Example

#include <stdio.h>

#include <string.h>

int main()

char name[] = "Pratima";

int length;

length = strlen(name);

printf("Length of string = %d", length);

return 0;}

2. User-Defined Functions
User-defined functions are created by the programmer to perform a specific task.

Example

#include <stdio.h>

void add()

int a = 5, b = 3;

printf("Sum = %d", a + b);

}
int main()

add();

return 0;

User-Defined Functions are Further Classified Into 4 Types

1. Function with no arguments and no return value

Example:1

#include <stdio.h>

/* User-defined function */

void sum()

int a = 5, b = 3;

printf("Sum = %d", a + b);

int main()

sum(); // Function call

return 0;

Exapmle2:

#include <stdio.h>

/* User-defined function */

void sum()

int a, b, result;

printf("Enter first number: ");

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

scanf("%d", &b);

result = a + b;

printf("Sum = %d\n", result);

int main()

sum(); // Function call

return 0;

2. Function with arguments and no return value

Example1:

#include <stdio.h>

void sum(int a, int b)

printf("Sum = %d", a + b);

int main()

sum(5, 3);

return 0;

Example:2

#include <stdio.h>

/* User-defined function with arguments */

void sum(int a, int b)


{

result = a + b;

printf("Sum = %d\n", result);

int main()

int x, y;

printf("Enter first number: ");

scanf("%d", &x);

printf("Enter second number: ");

scanf("%d", &y);

sum(x, y); // Passing arguments to function

return 0;

3. Function with no arguments but return value

Example:1

#include <stdio.h>

int sum()

int a = 5, b = 3;

return a + b;

int main()

int result;

result = sum();

printf("Sum = %d", result);

return 0;}
Example:2

#include <stdio.h>

/* User-defined function */

int sum()

int a, b;

printf("Enter first number: ");

scanf("%d", &a);

printf("Enter second number: ");

scanf("%d", &b);

return a + b; // Returning value

int main()

int result;

result = sum(); // Function call

printf("Sum = %d\n", result);

return 0;}

4. Function with arguments and return value

Example1:

#include <stdio.h>

int sum(int a, int b)

return a + b;

int main()

{
int result;

result = sum(5, 3);

printf("Sum = %d", result);

return 0;

Example:2

#include <stdio.h>

/* User-defined function with arguments */

void sum(int a, int b)

int result;

result = a + b;

return result; // Returning value to main

int main()

int x, y, answer;

printf("Enter first number: ");

scanf("%d", &x);

printf("Enter second number: ");

scanf("%d", &y);

answer = sum(x, y); // Passing arguments

printf("Sum = %d\n", answer);

return 0;

}
❖ Standard and Categories of Functions
1. Standard Functions

Standard functions are predefined functions provided by the C language and stored in header files.

Examples of Standard Functions

Function Purpose Header File

printf() Output <stdio.h>

scanf() Input <stdio.h>

strlen() String length <string.h>

sqrt() Square root <math.h>

Example

#include <stdio.h>

#include<math.h>

int main()

Float n=25,result;

Result = sqrt(n);

printf("Square root =%f" result);

return 0;

❖ Parameter Passing Techniques in C


Parameter passing means how values are passed from calling function to called function.

Types of Parameter Passing

1. Call by Value

• Copy of variable is passed

• Changes do not affect original variable


Example

#include <stdio.h>

void change(int x)

x = x + 10;

printf("Value inside function = %d\n", x);

int main()

int a = 5;

change(a);

printf("Value inside main = %d\n", a);

return 0;

Output:

Value inside function = 15

Value inside main = 5

2. Call by Reference

• Address of variable is passed

• Changes affect original variable

Example

#include <stdio.h>

void change(int *x)

*x = *x + 10;

printf("Value inside function = %d\n", *x);

int main()
{

int a = 5;

change(&a); // passing address

printf("Value inside main = %d\n", a);

return 0;

Output:

Value inside function = 15

Value inside main = 15

Simple Factorial program:

#include <stdio.h>

int main()
{
int n, i, fact = 1;

printf("Enter a number: ");


scanf("%d", &n);

for(i = 1; i <= n; i++)


{
fact = fact * i;
}

printf("Factorial = %d", fact);

return 0;
}
Recursion
Recursion means a function calling itself to solve a problem.

Example: Factorial Using Recursion

#include <stdio.h>

int fact(int n)

if(n == 0 || n == 1) // Base Case

return 1;

return n * fact(n - 1); // Recursive Call

int main()

int num, result;

printf("Enter a number: ");

scanf("%d", &num);

result = fact(num);

printf("Factorial = %d", result);

return 0;

You might also like