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

Lesson 8 - Functions

The document discusses functions in C programming. It defines what functions are, their uses, and different types of functions. It also covers topics like parameters, return types, scope of variables, and passing arrays to functions.
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)
8 views

Lesson 8 - Functions

The document discusses functions in C programming. It defines what functions are, their uses, and different types of functions. It also covers topics like parameters, return types, scope of variables, and passing arrays to functions.
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
You are on page 1/ 22

Functions

Function
A function is a block of code that performs specific task.

Modularity

Reusability

Abstraction
Two Types of Function

Standard Library User-Defined


Functions Functions
Standard Library (Predefined)
Functions
Standard Library (Predefined)

Functions Header

printf() stdio.h

scanf() stdio.h

sqrt() math.h

pow() math.h

main()
User-defined Functions
User-defined Functions

Function Declaration, Definition, and Calling

Parameters and Arguments

Return Types and Void Functions

Scope of Variables, Local and Global Variables

Passing Arrays to Functions


User-defined Functions
User-defined functions are a block of code written by the user to perform a specific action.

Syntax

return_type function_name(parameters) {
//code to be executed
}
User-defined Function Examples
void greet() {
printf(“Happy Birthday!\n”);
}
User-defined Function Examples
#include <stdio.h>

// Function declaration
void greet();

int main() {
greet(); //Function call
return 0;
}

//Function definition
void greet(){
printf(“Happy Birthday!\n”);
}
Parameters and Arguments
Syntax

return_type function_name(parameter1, parameter2, parameter3) {


//code to be executed
}
Parameters and Arguments Examples
#include <stdio.h>

void greet(char name[]);

int main() {
greet("James");
return 0;
}

void greet(char name[]){


printf("Happy Birthday, %s!\n",name);
}
Parameters and Arguments Examples
#include <stdio.h>

void greet(char name[]);

int main() {
greet("James");
greet(”David");
greet(”Mary");
return 0;
}

void greet(char name[]){


printf("Happy Birthday, %s!\n",name);
}
Parameters and Arguments Example
#include <stdio.h>

void greet(char name[], int age);

int main() {
greet("James", 23);
greet("David",24);
greet("Mary",25);
return 0;
}

void greet(char name[], int age){


printf("Happy Birthday, %s!\n",name);
printf(“You are now %d years old.\n\n”,age);
}
Return Types and Void Functions
Return Types
A function may return a value. The return type of the function defines the data type of the
value the function will return. Common return types include int, float, char, and double.
Void Functions
If a function does not return a value, its return type is void. These functions perform an
action but do not return a value to the caller
Scopes of Variables

Local Variables

Global Variables
Local Variable Example
#include <stdio.h>

void display();

int main() {
display();
//printf("%d", localVariable); // will throw an error.
return 0;
}

void display() {
int localVariable = 10; // Local variable
printf("Local variable inside function: %d\n", localVariable);
}
Global Variable Example
#include <stdio.h>

int globalVariable = 20; // Global variable


void display();

int main() {
printf("Global variable in main: %d\n", globalVariable);
display();
return 0;
}
void display() {
printf("Global variable inside display function: %d\n", globalVariable);
}
Passing Arrays to Functions

In C, arrays cannot be passed by value to a function. However, you can


pass them by reference, which involves passing the address of the array to
the function.
Passing Arrays to Functions
#include <stdio.h>

void printArray(int arr[], int size);

int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5);
return 0;
}

void printArray(int arr[], int size) {


for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
Exercise 1: Balance and Interest Rate
You are developing a program for a bank. You need to write a function that
takes the balance and interest rate as input, and returns the interest earned
for a year.
Exercise 1: Example
INPUT: OUTPUT:
Balance: 2000 Interest earned: Php 720.00
Interest Rate: 3

You might also like