
![]() |
@wtf | |
What is a function |
||
1
Replies
9
Views
1 Bookmarks
|
![]() |
@wtf | 12 days |
*What is a Function?* A function is a reusable block of code that performs a specific task. Think of it as a machine: you give it some input (arguments), it processes something, and then gives you output. ✅ *Defining a Function* Here’s the basic structure: def greet(): print(Hello, Python learner!) To call or run the function: greet() *Output:* Hello, Python learner! ✅ *Function with Parameters* You can make your function accept values using parameters: def greet(name): print(fHello, name!) greet(Sam) *Output:* Hello, Sam! ✅ *Return Values* Functions can return values instead of just printing them: def add(a, b): return a + b result = add(3, 5) print(result) *Output:* 8 *Mini Project: Prime Number Checker* Let’s use what we learned to create a small, useful function! def is_prime(n): if n 2: return False for i in range(2, int(n**0.5)+1): if n % i == 0: return False return True Try it! num = int(input(Enter a number: )) if is_prime(num): print(It's a prime number.) else: print(Not a prime number.) *This project introduces:* - Logic inside a function - Conditional checks - Looping through a range |
||


